Submission #107214

#TimeUsernameProblemLanguageResultExecution timeMemory
107214nxteruPalembang Bridges (APIO15_bridge)C++14
100 / 100
131 ms6884 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define PB push_back struct t{ ll a,b; bool operator<(const t&q)const{ return a+b<q.a+q.b; } }; struct mid{ priority_queue<ll>d; priority_queue<ll,vector<ll>,greater<ll> >u; ll ds,us; void ini(void){ ds=0,us=0; while(!d.empty())d.pop(); while(!u.empty())u.pop(); } void pushd(ll x){ d.push(x); ds+=x; } void pushu(ll x){ u.push(x); us+=x; } ll popd(void){ ll res=d.top(); d.pop(); ds-=res; return res; } ll popu(void){ ll res=u.top(); u.pop(); us-=res; return res; } void push(ll x){ if(u.size()==0||u.top()<=x)pushu(x); else pushd(x); while(u.size()>d.size()+1)pushd(popu()); while(u.size()<d.size())pushu(popd()); } ll que(void){ ll res=us-ds; if(u.size()>d.size())res-=u.top(); return res; } }; int n,k; ll s,l[100005],r[100005]; vector<t>p; mid q; int main(void){ scanf("%d%d",&k,&n); for(int i=0;i<n;i++){ char x,y; ll a,b; scanf(" %c %lld %c %lld",&x,&a,&y,&b); if(x==y)s+=abs(a-b); else{ p.PB(t{a,b}); s++; } } n=p.size(); sort(p.begin(),p.end()); q.ini(); for(int i=0;i<n;i++){ q.push(p[i].a); q.push(p[i].b); l[i]=q.que(); } q.ini(); for(int i=n-1;i>=0;i--){ q.push(p[i].a); q.push(p[i].b); r[i]=q.que(); } ll ans=r[0]; for(int i=0;i<n;i++)ans=min(ans,l[i]+r[i+1]); if(k==1)printf("%lld\n",l[n-1]+s); else printf("%lld\n",ans+s); }

Compilation message (stderr)

bridge.cpp: In function 'int main()':
bridge.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&k,&n);
     ~~~~~^~~~~~~~~~~~~~
bridge.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c %lld %c %lld",&x,&a,&y,&b);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...