# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
64276 | kingpig9 | Palembang Bridges (APIO15_bridge) | C++11 | 4 ms | 1124 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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
bool cmp (pii p1, pii p2) {
return p1.fi + p1.se < p2.fi + p2.se;
}
int K, N;
vector<pll> A;
void go (vector<ll> &vres) {
multiset<ll> st = {A[0].fi, A[0].se};
set<ll>::iterator it = st.begin();
ll ans = A[0].se - A[0].fi;
vres.push_back(ans);
for (int i = 1; i < A.size(); i++) {
ll x = A[i].fi, y = A[i].se;
st.insert(x);
st.insert(y);
if (y < *it) {
ans += 2 * (*it) - x - y;
it--;
} else if (x > *it) {
it++;
ans += x + y - 2 * (*it);
} else {
ans += y - x;
}
vres.push_back(ans);
}
}
vector<ll> res[2];
int main() {
scanf("%d %d", &K, &N);
ll ans = 0;
for (int i = 0; i < N; i++) {
int x, y;
char c, d;
scanf(" %c %d %c %d", &c, &x, &d, &y);
if (x > y) {
swap(x, y);
}
if (c == d) {
ans += y - x;
} else {
ans++;
A.push_back({x, y});
}
}
if (!A.empty()) {
sort(all(A), cmp);
go(res[0]);
if (K == 1) {
printf("%lld\n", ans + res[0].back());
return 0;
}
reverse(all(A));
go(res[1]);
ll mnadd = min(res[0].back(), res[1].back());
for (int i = 0; i + 1 < A.size(); i++) {
mnadd = min(mnadd, res[0][i] + res[1][A.size() - 2 - i]);
}
ans += mnadd;
}
printf("%lld\n", ans);
}
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... |