제출 #466659

#제출 시각아이디문제언어결과실행 시간메모리
466659cgiosyPalembang Bridges (APIO15_bridge)C++17
0 / 100
1 ms204 KiB
#include <bits/stdc++.h> #define clear(X) decltype(X)().swap(X); using namespace std; using ll=long long; struct pii { int l, r; bool operator<(pii b) const { return l+r<b.l+b.r; } }; int main() { ios::sync_with_stdio(0);cin.tie(0); int K, N, M=0; cin>>K>>N; pii A[N]; ll s=0; 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) A[M++]={l, r}; else s+=r-l; } N=M; s+=N; sort(A, A+N); ll B[N], w=0; priority_queue<int> L; priority_queue<int, vector<int>, greater<int>> R; auto balance=[&] { if(L.size()>R.size()) { int v=L.top(); L.pop(), R.push(v); w+=v*2; } else if(L.top()>R.top()) { int l=L.top(), r=R.top(); L.pop(), L.push(r); R.pop(), R.push(l); w+=(l-r)*2; } }; for(int i=0; i<N; i++) { auto[l,r]=A[i]; L.push(l), w-=l; balance(); L.push(r), w-=r; balance(); B[i]=w; } w=0; clear(L); clear(R); ll t=B[N-1]; if(K==2 && N) for(int i=N; --i;) { auto[l,r]=A[i]; L.push(l), w-=l; balance(); L.push(r), w-=r; balance(); t=min(t, w+B[i-1]); } cout<<s+t<<'\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...