#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;
typedef pair<ll,ll> pll;
ll cnt[100010][3];
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);
}
}
}
// cout<<ans<<"\n";
ll now[]={0,0,0};
for (int i=1;i<=n;i++) {
now[1]+=cnt[i][1];
now[2]+=cnt[i][2];
}
for (int i=1;i<=n;i++) {
pll ch={1000,1000};
ll vu=-1;
now[1]-=cnt[i][1];
now[2]-=cnt[i][2];
for (int up=0;up<=2;up++) {
ll dn=2-up;
ll du=up-cnt[i][2];
ll dd=dn-cnt[i][1];
if (now[2]-du<0 or now[1]-dd<0) continue;
int cst=abs(du)+abs(dd)+(up!=1);
if (ch.F>cst or (ch.F==cst and ch.S>abs(now[2]-du-now[1]+dd))) {
ch.F=cst;
ch.S=abs(now[2]-du-now[1]+dd);
vu=up;
}
}
ll vd=2-vu;
ll du=vu-cnt[i][2],dd=vd-cnt[i][1];
ans+=abs(du)+abs(dd);
cnt[i][2]+=du;cnt[i+1][2]-=du;now[2]-=du;
cnt[i][1]+=dd;cnt[i+1][1]-=dd;now[1]-=dd;
}
// cout<<ans<<"\n";
for (int i=1;i<=n;i++) {
// cout<<cnt[i][1]<<" "<<cnt[i][2]<<" cnt\n";
ans+=cnt[i][1]!=1;
}
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... |