Submission #982931

#TimeUsernameProblemLanguageResultExecution timeMemory
982931vjudge1Palembang Bridges (APIO15_bridge)C++17
22 / 100
31 ms2524 KiB
#include<bits/stdc++.h> #define f first #define s second #define pb push_back #define pii pair<int,int> #define pll pair<ll,ll> #define ll long long #define sz(x) (ll)x.size() using namespace std; int n; void sv1(){ ll ans=0;vector<ll>vec; for(int i=1;i<=n;i++){ char X,Y;ll x,y; cin>>X>>x>>Y>>y; if(X==Y)ans+=abs(y-x); else vec.pb(x),vec.pb(y),ans++; }sort(vec.begin(),vec.end()); ll med=vec[(sz(vec)-1)/2]; for(auto it : vec)ans+=abs(med-it); cout<<ans;return; } bool cmp(pll a,pll b){ return a.f+a.s<b.f+b.s; } void sv2(){ ll ans=0;vector<pll>v; for(int i=1;i<=n;i++){ char X,Y;ll x,y; cin>>X>>x>>Y>>y; if(x<y)swap(x,y); if(X==Y)ans+=abs(y-x); else v.pb({x,y}),ans++; }sort(v.begin(),v.end(),cmp); priority_queue<ll>lq; priority_queue<ll,vector<ll>,greater<ll>>rq; ll dl[sz(v)+2]={0},dr[sz(v)+2]={0}; ll sl=0,sr=0; for(int i=1;i<=sz(v);i++){ lq.push(v[i-1].f),lq.push(v[i-1].s); sl+=v[i-1].f+v[i-1].s; while(!lq.empty()&&!rq.empty()&&lq.top()>rq.top()){ sl-=lq.top();sr+=lq.top();rq.push(lq.top());lq.pop(); } while(sz(lq)>sz(rq)+1){ sl-=lq.top();sr+=lq.top();rq.push(lq.top());lq.pop(); } while(sz(rq)>sz(lq)){ sr-=rq.top();sl+=rq.top();lq.push(rq.top());rq.pop(); } dl[i]=sz(lq)*lq.top()-sl+sr-sz(rq)*lq.top(); cout<<lq.top()<<'\n'; }while(!lq.empty())lq.pop();while(!rq.empty())rq.pop();sl=0,sr=0; for(int i=sz(v);i>=1;i--){ lq.push(v[i-1].f),lq.push(v[i-1].s); sl+=v[i-1].f+v[i-1].s; while(!lq.empty()&&!rq.empty()&&lq.top()>rq.top()){ sl-=lq.top();sr+=lq.top();rq.push(lq.top());lq.pop(); } while(sz(lq)>sz(rq)+1){ sl-=lq.top();sr+=lq.top();rq.push(lq.top());lq.pop(); } while(sz(rq)>sz(lq)){ sr-=rq.top();sl+=rq.top();lq.push(rq.top());rq.pop(); }dr[i]=sz(lq)*lq.top()-sl+sr-sz(rq)*lq.top(); }ll res=1e18; for(int i=0;i<=sz(v);i++)res=min(res,dl[i]+dr[i+1]); cout<<res+ans;return ; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int k;cin>>k>>n; if(k==1)sv1();else sv2(); 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...