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;
using nagai = long long;
#define sz(x) int((x).size())
const int N=200200;
int cnt[N][2];
int dist(pair<int,int>a,pair<int,int>b){
return abs(a.first-b.first)+abs(a.second-b.second);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
nagai ansich=0;
for(int i=0;i<2*n;++i){
int x,y;
cin>>x>>y;
int x1=x,y1=y;
if(x<1)x1=1;
if(x>n)x1=n;
if(y<1)y1=1;
if(y>2)y1=2;
ansich+=abs(x-x1);
ansich+=abs(y-y1);
++cnt[x1-1][y1-1];
}
int sum1=-n;
for(int i=0;i<n;++i)sum1+=cnt[i][0];
if(sum1<0){
sum1*=-1;
for(int i=0;i<n;++i)swap(cnt[i][0],cnt[i][1]);
}
for(int i=0;i<n;++i){
while(cnt[i][1]>1&&cnt[i][0]<1)++cnt[i][0],--cnt[i][1],++ansich;
while(cnt[i][1]<1&&cnt[i][0]>1)++cnt[i][1],--cnt[i][0],++ansich;
if(cnt[i][0]<1)ansich+=(1-cnt[i][0]),cnt[i+1][0]-=(1-cnt[i][0]);
if(cnt[i][0]>1)ansich+=cnt[i][0]-1,cnt[i+1][0]+=cnt[i][0]-1;
if(cnt[i][1]>1)ansich+=cnt[i][1]-1,cnt[i+1][1]+=cnt[i][1]-1;
}
cout<<ansich<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |