# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
490847 | hollwo_pelw | Palembang Bridges (APIO15_bridge) | C++17 | 1 ms | 332 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
if (fopen(filein.c_str(), "r")){
freopen(filein.c_str(), "r", stdin);
freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
freopen(fileerr.c_str(), "w", stderr);
#endif
}
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main(){
#ifdef hollwo_pelw_local
FAST_IO("input.inp", "output.out", "error.err");
auto start = chrono::steady_clock::now();
#else
FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
return 0;
}
const int N = 1e6 + 5;
#define int long long
int n, k, extra, pre[N];
struct person_t {
int a, b;
person_t (int _a = 0, int _b = 0)
: a(_a), b(_b) {}
bool operator < (const person_t &o) const {
return a + b < o.a + o.b;
}
};
vector<person_t> v;
int rtot, ltot;
priority_queue<int> pql, pqr;
inline void clear() {
rtot = ltot = 0;
while (pql.size()) pql.pop();
while (pqr.size()) pqr.pop();
}
inline void insert(int v) {
// cout << "insert " << v << '\n';
rtot += v, pqr.push(-v);
while (pql.size() < pqr.size()) {
rtot -= - pqr.top();
pql.push(- pqr.top());
ltot += - pqr.top();
pqr.pop();
}
}
inline int get() {
if (pql.size() != pqr.size()) {
assert(pql.size() == pqr.size() + 1);
return rtot - ltot + pql.top();
}
return rtot - ltot;
}
void Hollwo_Pelw() {
cin >> k >> n;
for (int i = 1, x, y; i <= n; i++) {
char p, q; cin >> p >> x >> q >> y;
if (p == q) {
// same side
extra += abs(x - y);
} else {
v.emplace_back(x, y);
}
}
n = v.size();
extra += n;
if (!n) return cout << extra, (void) 0;
clear();
for (int i = 0; i < n; i++) {
insert(v[i].a);
insert(v[i].b);
pre[i] = get();
}
int res = pre[n - 1];
if (k == 2) {
clear();
for (int i = n - 1; i; i--) {
insert(v[i].a);
insert(v[i].b);
res = min(res, get() + pre[i - 1]);
}
}
cout << res + extra;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |