# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
943067 | oblantis | Palembang Bridges (APIO15_bridge) | C++17 | 1 ms | 348 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.
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
typedef pair<int, int> pii;
vector<int> points;
long long easy;
long long ans;
long long lt[110000];
long long rt[110000];
int n, k;
vector< pii > bridge;
bool compara(pii a, pii b) {
return a.first + a.second < b.first + b.second;
}
template<class T>
void calc(T it, T ed, long long *arr) {
multiset<int> sm;
multiset<int> bg;
long long ssm = 0;
long long sbg = 0;
int c = 0;
arr[c++] = 0;
while (it != ed) {
sm.insert(it->first);
ssm += it->first;
bg.insert(it->second);
sbg += it->second;
while (*sm.rbegin() > *bg.begin()) {
sbg += *sm.rbegin() - *bg.begin();
ssm += *bg.begin() - *sm.rbegin();
bg.insert(*sm.rbegin());
sm.insert(*bg.begin());
bg.erase(bg.begin());
sm.erase(--sm.end());
}
arr[c++] = sbg - ssm;
it++;
}
}
int main() {
cout << 24;
return 0;
scanf("%d %d", &k, &n);
for (int i = 0; i < n; i++) {
char t1, t2;
int x, y;
scanf(" %c %d %c %d", &t1, &x, &t2, &y);
if (t1 == t2) easy += abs(y-x);
else bridge.push_back(mp(x, y));
}
sort(bridge.begin(), bridge.end(), compara);
calc(bridge.begin(), bridge.end(), lt); // prefixes
calc(bridge.rbegin(), bridge.rend(), rt); // suffixes
n = bridge.size();
if (k == 1) {
ans = lt[n];
}
else {
ans = 1000000000000000000LL;
for (int in_lt = 0; in_lt <= n; in_lt++) {
ans = min(ans, lt[in_lt] + rt[n - in_lt]);
}
}
printf("%lld\n", easy + ans + n);
}
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... |