Submission #566110

#TimeUsernameProblemLanguageResultExecution timeMemory
566110HanksburgerPalembang Bridges (APIO15_bridge)C++17
100 / 100
88 ms7916 KiB
#include <bits/stdc++.h> #define int long long using namespace std; int pref[100005], l, r, k, n, num, ans=1e18; priority_queue<int> pql, pqr; vector<pair<int, int> > vec; void f(int x) { int median=pql.size()?pql.top():1e9; if (x<=median) { pql.push(x); l+=x; } else { pqr.push(-x); r+=x; } if (pql.size()>=pqr.size()+2) { x=pql.top(); pql.pop(); pqr.push(-x); l-=x; r+=x; } else if (pql.size()<pqr.size()) { x=-pqr.top(); pqr.pop(); pql.push(x); r-=x; l+=x; } } bool cmp(pair<int, int> x, pair<int, int> y) { return x.first+x.second<y.first+y.second; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> k >> n; for (int i=0; i<n; i++) { char p, q; int x, y; cin >> p >> x >> q >> y; if (p==q) num+=abs(x-y); else vec.push_back({x, y}); } sort(vec.begin(), vec.end(), cmp); n=vec.size(); num+=n; for (int i=0; i<n; i++) { f(vec[i].first); f(vec[i].second); pref[i]=r-l; } if (!n || k==1) { cout << num+pref[n-1]; return 0; } while (pql.size()) pql.pop(); while (pqr.size()) pqr.pop(); l=r=0; for (int i=n-1; i>=0; i--) { ans=min(ans, r-l+pref[i]); f(vec[i].first); f(vec[i].second); } cout << num+ans; 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...