Submission #989607

#TimeUsernameProblemLanguageResultExecution timeMemory
989607aykhnPalembang Bridges (APIO15_bridge)C++17
100 / 100
76 ms8820 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define inf 0x3F3F3F3F3F3F3F3F signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int k, n, S = 0; cin >> k >> n; vector<array<int, 2>> v; for (int i = 0; i < n; i++) { char a, b; int l, r; cin >> a >> l >> b >> r; if (l > r) swap(l, r); if (a == b) S += r - l; else v.push_back({l, r}); } if (v.empty()) { cout << S << '\n'; return 0; } n = v.size(); sort(v.begin(), v.end(), [&](const array<int, 2> &a, const array<int, 2> &b) { return a[0] + a[1] < b[0] + b[1]; }); vector<int> bs(n, 0), bp(n, 0); priority_queue<int> L; priority_queue<int, vector<int>, greater<int>> R; int sL = 0, sR = 0; for (int i = 0; i < n; i++) { L.push(v[i][0]), L.push(v[i][1]); sL += v[i][0] + v[i][1]; while (!L.empty() && !R.empty() && L.top() > R.top()) R.push(L.top()), sR += L.top(), sL -= L.top(), L.pop(); while (L.size() > R.size()) R.push(L.top()), sR += L.top(), sL -= L.top(), L.pop(); while (L.size() < R.size()) L.push(R.top()), sL += R.top(), sR -= R.top(), R.pop(); bp[i] = sR - sL; } while (!L.empty()) L.pop(); while (!R.empty()) R.pop(); sL = sR = 0; for (int i = n - 1; i >= 0; i--) { L.push(v[i][0]), L.push(v[i][1]); sL += v[i][0] + v[i][1]; while (!L.empty() && !R.empty() && L.top() > R.top()) R.push(L.top()), sR += L.top(), sL -= L.top(), L.pop(); while (L.size() > R.size()) R.push(L.top()), sR += L.top(), sL -= L.top(), L.pop(); while (L.size() < R.size()) L.push(R.top()), sL += R.top(), sR -= R.top(), R.pop(); bs[i] = sR - sL; } if (k == 1 || v.size() == 1) cout << bp[n - 1] + S + n << '\n'; else { int res = bp[n - 1]; for (int i = 0; i + 1 < n; i++) res = min(res, bp[i] + bs[i + 1]); cout << res + S + n << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...