#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using ll = long long;
const ll mod=1e9+7;
const int nx=2e5+5;
bool cmp(ii a, ii b)
{
return a.se<b.se;
}
int n, c[2][nx];
ii a[nx];
ll res=0;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n;
for(int i = 1; i <= 2*n; i++)
{
cin>>a[i].fi>>a[i].se;
int x=a[i].fi, y=a[i].se;
if(a[i].fi<1) x=1;
else if(a[i].fi>n) x=n;
if(a[i].se<1) y=1;
else if(a[i].se>2) y=2;
res+=abs(a[i].fi-x)+abs(a[i].se-y);
c[y-1][x]++;
}
// for(int i = 0; i < 2; i++)
// for(int j = 1; j <= n; j++)
// cout<<c[i][j]<<' ';
for(int i = 1; i <= n; i++)
{
if(c[1][i]>1 && c[0][i]<1)
{
int add=min(1-c[0][i], c[1][i]-1);
res+=add;
c[0][i]+=add;
c[1][i]-=add;
}
if(c[0][i]>1 && c[1][i]<1)
{
int add=min(1-c[1][i], c[0][i]-1);
res+=add;
c[1][i]+=add;
c[0][i]-=add;
}
for(int j = 0; j < 2; j++)
{
if(c[j][i]<1) res+=(1-c[j][i]), c[j][i+1]-=(1-c[j][i]), c[j][i]=1;
if(c[j][i]>1) res+=(c[j][i]-1), c[j][i+1]+=(c[j][i]-1), c[j][i]=1;
}
}
cout<<res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |