#include<bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) x.begin()+1, x.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
ll cnt[100010][2];
int main() {
speed;
ll n;
cin>>n;
ll ans=0;
memset(cnt,0,sizeof(cnt));
for (ll i=1;i<=2*n;i++) {
ll x,y;
cin>>x>>y;
if (x<=1) {
if (y<=1) {
ans+=(1-x)+(1-y);
cnt[1][1]++;
} else {
cnt[1][2]++;
ans+=(1-x)+(y-2);
}
} else if (n<=x) {
if (y<=1) {
cnt[n][1]++;
ans+=(x-n)+(1-y);
} else {
cnt[n][2]++;
ans+=(x-n)+(y-2);
}
} else {
if (y<=1) {
cnt[x][1]++;
ans+=(1-y);
} else {
cnt[x][2]++;
ans+=(y-2);
}
}
}
for (int i=1;i<=n;i++) {
if (cnt[i][1]==0 and cnt[i][2]>1) {
cnt[i][2]--;
cnt[i][1]++;
ans++;
} else if (cnt[i][2]==0 and cnt[i][1]>1) {
cnt[i][2]++;
cnt[i][1]--;
ans++;
}
}
ll one=0;
for (ll i=1;i<=n;i++) {
one+=cnt[i][1];
cnt[i][1]+=cnt[i][2];
}
ans+=abs(n-one);
ll pre=0;
for (ll i=1;i<n;i++) {
pre+=cnt[i][1];
ans+=abs(pre-i*2);
}
cout<<ans<<"\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... |