#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin>>n;
int cnt[n+1][3];
memset(cnt,0,sizeof cnt);
ll ans=0;
for(int i=1;i<=2*n;i++){
int x,y;
cin>>x>>y;
if(x<1){
ans+=1-x;
x=1;
}
if(x>n){
ans+=x-n;
x=n;
}
if(y<1){
ans+=1-y;
y=1;
}
if(y>2){
ans+=y-2;
y=2;
}
cnt[x][y]++;
}
vector<int>left(3,0),now(3,1);
for(int i=1;i<=n;i++){
for(int j=1;j<=2;j++){
left[j]+=cnt[i][j]-1;
}
for(int j=1;j<=2;j++){
if(left[j]>0&&left[3-j]<0){
int f=min(left[j],-left[3-j]);
ans+=f;
left[j]-=f;
left[3-j]+=f;
}
}
for(int j=1;j<=2;j++){
ans+=abs(left[j]);
}
}
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... |