#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<int,pii>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
void solve(){
int n;
cin >> n;
pii arr[2*n];
int counter=0;
int cnt[n+5][2];
memset(cnt,0,sizeof(cnt));
for(int x=0;x<2*n;x++){
cin >> arr[x].first >> arr[x].second;
arr[x].second--;
if(arr[x].first<1){
counter+=1-arr[x].first;
arr[x].first=1;
}
else if(arr[x].first>n){
counter+=arr[x].first-n;
arr[x].first=n;
}
if(arr[x].second<0){
counter+=0-arr[x].second;
arr[x].second=0;
}
else if(arr[x].second>1){
counter+=arr[x].second-1;
arr[x].second=1;
}
cnt[arr[x].first][arr[x].second]++;
}
//for(int x=1;x<=n;x++){
//cout << cnt[x][0] << " ";
//}
//cout << "\n";
//for(int x=1;x<=n;x++){
//cout << cnt[x][1] << " ";
//}
//cout << "\n";
int storage[n+5][2];
int storage2[n+5][2];
memset(storage,0,sizeof(storage));
memset(storage2,0,sizeof(storage2));
for(int x=1;x<=n;x++){
while(cnt[x][0]>1){
storage[x][0]++;
cnt[x][0]--;
}
if(cnt[x][0]==0){
storage2[x][0]++;
}
while(cnt[x][1]>1){
storage[x][1]++;
cnt[x][1]--;
}
if(cnt[x][1]==0){
storage2[x][1]++;
}
}
pii a={0,0}; //storage
pii b={0,0}; //storage2
for(int x=1;x<=n;x++){
a.first+=storage[x][0];
a.second+=storage[x][1];
b.first+=storage2[x][0];
b.second+=storage2[x][1];
int mini;
//a[0] b[0]
mini=min(b.first,a.first);
a.first-=mini;
b.first-=mini;
//a[1] b[1];
mini=min(b.second,a.second);
a.second-=mini;
b.second-=mini;
mini=min(a.first,b.second);
a.first-=mini;
b.second-=mini;
counter+=mini;
mini=min(a.second,b.first);
a.second-=mini;
b.first-=mini;
counter+=mini;
counter+=a.first+a.second+b.first+b.second;
}
cout << counter;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |