Submission #380793

#TimeUsernameProblemLanguageResultExecution timeMemory
380793jass921026Palembang Bridges (APIO15_bridge)C++14
22 / 100
106 ms14420 KiB
#include<bits/stdc++.h> using namespace std; #define jizz ios_base::sync_with_stdio(false);cin.tie(NULL); typedef long long ll; typedef pair<int,int> pii; #define F first #define S second #define ALL(x) (x).begin(),(x).end() #define pb push_back #define mkp make_pair bool cmp(pii a, pii b){ return a.F+a.S<b.F+b.S; } int abs(int x){ return x>0?x:-x; } vector<pii> segs, pos; map<int,int> ls, lb, rs, rb; int main(){ jizz int k, n; cin>>k>>n; ll tmp=0; for(int i=0;i<n;i++){ char p, q; int s, t; cin>>p>>s>>q>>t; if(s>t) swap(s,t); tmp+=(t-s); if(p!=q) { tmp++; segs.pb(mkp(s,t)); pos.pb(mkp(s,0)); pos.pb(mkp(t,1)); } } ll ans=tmp; if(segs.empty()){ cout<<tmp<<"\n"; return 0; } sort(ALL(segs),cmp); sort(ALL(pos)); int sz=segs.size(); ll lsum=0, rsum=0; for(int i=0;i<sz;i++){ rs[pos[i].F]++; if(pos[i].S==1) rsum-=2*(pos[i].F); } for(int i=sz;i<sz*2;i++){ rb[pos[i].F]++; if(pos[i].S==0) rsum+=2*(pos[i].F); } if(k==1){ cout<<ans+lsum+rsum<<"\n"; return 0; } ll ans2=ans+lsum+rsum; for(int i=0;i<sz;i++){ rs[segs[i].F]--; if(segs[i].S<rb.begin()->F){ rs[segs[i].S]--; rsum+=2*segs[i].S; auto p=rb.begin(); rsum-=2*(p->F); rb[p->F]--; rs[p->F]++; } else{ rb[segs[i].S]--; } lb[segs[i].S]++; if(segs[i].F<lb.begin()->F){ ls[segs[i].F]++; } else{ lb[segs[i].F]++; lsum+=2*segs[i].F; auto p=lb.begin(); lsum-=2*(p->F); lb[p->F]--; ls[p->S]++; } ans2=min(ans2,ans+lsum+rsum); } cout<<ans2<<"\n"; return 0; } /* 1 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7 2 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7 2 3 A 3 B 3 A 4 B 4 A 5 B 5 */
#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...