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<iostream>
#include<math.h>
using namespace std;
const int N=1e5+2;
int cnt[N][4];
int main(){
int n,i,j,k,l;
long long ans=0;
cin>>n;
for(i=1;i<=2*n;i++){
cin>>j>>k;
if(j>n){
ans+=j-n;
j=n;
}
if(j<1){
ans+=1-j;
j=1;
}
if(k>2){
ans+=k-2;
k=2;
}
if(k<1){
ans+=1-k;
k=1;
}
cnt[j][k]++;
}
j=0;
k=0;
//cout<<ans<<endl;
for(i=1;i<=n;i++){
// cout<<j<<" "<<k<<" "<<cnt[i][1]<<" "<<cnt[i][2]<<" "<<ans<<endl;
j+=cnt[i][1];
k+=cnt[i][2];
j--;
k--;
if(j>0&&k<0){
l=min(j,-k);
ans+=l;
j-=l;
k+=l;
}
if(k>0&&j<0){
l=min(-j,k);
ans+=l;
k-=l;
j+=l;
}
ans+=abs(j)+abs(k);
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |