# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
107214 | nxteru | Palembang Bridges (APIO15_bridge) | C++14 | 131 ms | 6884 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |