# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
749085 | 1075508020060209tc | Coin Collecting (JOI19_ho_t4) | C++14 | 179 ms | 7484 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;
#define int long long
int n;
int freq[4][500005];
int ps[3][500005];
int ans;
signed main(){
cin>>n;
ans=0;
for(int i=1;i<=n*2;i++){
int a;int b;
cin>>a>>b;
if(b<=1){
ans+=abs(b-1);
b=1;
}else{
ans+=abs(b-2);
b=2;
}
if(a<=1){
ans+=abs(1-a);
a=1;
}
if(a>=n){
ans+=abs(n-a);
a=n;
}
freq[b][a]++;
}
for(int i=1;i<=n;i++){
ps[1][i]=ps[1][i-1]+freq[1][i];
ps[2][i]=ps[2][i-1]+freq[2][i];
ps[3][i]=ps[1][i]+ps[2][i];
}
int d1=0;
int d2=0;
for(int i=1;i<=n;i++){
d1+=freq[1][i]-1;
d2+=freq[2][i]-1;
if(d1>0&&d2<0){
int t=min(d1,-d2);
ans+=t;
d1-=t;d2+=t;
}
if(d1<0&&d2>0){
int t=min(-d1,d2);
ans+=t;
d1+=t;d2-=t;
}
ans+=abs(ps[3][i]-i*2);
}
cout<<ans<<endl;
}
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... |