Submission #990028

#TimeUsernameProblemLanguageResultExecution timeMemory
990028toastifishiPalembang Bridges (APIO15_bridge)C++14
0 / 100
1 ms348 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MAXN = 1e5 + 5; int n, k; ll lsum = 0, rsum = 0, pref[MAXN]; priority_queue <int> lpq; priority_queue <int, vector <int>, greater <int>> rpq; void insert(int x) { int median = (lpq.size() ? lpq.top() : 1000000001); if(x <= median) { lpq.push(x); lsum += x; } else { rpq.push(x); rsum += x; } if(rpq.size() + 1 < lpq.size()) { int nxt = lpq.top(); lpq.pop(); rpq.push(nxt); lsum -= nxt, rsum += nxt; } else if(lpq.size() < rpq.size()) { int nxt = rpq.top(); rpq.pop(); lpq.push(nxt); rsum -= nxt, lsum += nxt; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll ss = 0, ans = 0; cin >> k >> n; vector <pair <int, int>> v = {{0, 0}}; for(int i = 0; i < n; i++) { char a, b; int x, y; cin >> a >> x >> b >> y; if(a == b) ss += abs(x - y); else v.push_back({x, y}); } sort(v.begin(), v.end(), [&](pair <int, int> p, pair <int, int> q) { return p.first + p.second < q.first + q.second; }); n = (int)v.size(); ss += n - 1; cout << ss << "\n"; for(int i = 1; i < n; i++) { insert(v[i].first); insert(v[i].second); pref[i] = rsum - lsum; } ans = pref[n - 1]; if(k == 2) { while(lpq.size()) lpq.pop(); while(rpq.size()) rpq.pop(); lsum = rsum = 0; for(int i = n - 1; i > 0; i--) { insert(v[i].first); insert(v[i].second); ans = min(ans, rsum - lsum + pref[i - 1]); } } cout << ss + ans << "\n"; return 0; }
#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...