# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
107209 | nxteru | Palembang Bridges (APIO15_bridge) | C++14 | 152 ms | 6932 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 INF 1000000000000000000
#define PB push_back
struct t{
ll a,b;
bool operator<(const t&q)const{
return a+b<q.a+q.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 up(void){
pushu(popd());
}
void down(void){
pushd(popu());
}
void push(ll x){
if(u.size()==0||u.top()<=x)pushu(x);
else pushd(x);
while(u.size()>d.size()+1)down();
while(u.size()<d.size())up();
}
ll que(void){
ll res=us-ds;
if(u.size()>d.size())res-=u.top();
return res;
}
};
int n,k;
ll s,ans=INF,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();
}
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... |