#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
#define ll long long
#define fi first
#define se second
#define lf ((id<<1))
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define pb push_back
using namespace std;
const int MAXN = 1e5+10;
const int INF = 1e9+10;
const int LOG = 20;
int MOD;
typedef pair<int,int> pii;
int sum(int a, int b){ return (a+MOD+b)%MOD; }
void chsum(int &a, int b){ a = sum(a,b); }
int mul(int a, int b){ return (a*b)%MOD; }
int expo(int a, int b){
if(b==0) return 1;
int te = expo(a, b>>1); te = mul(te,te);
return (b%2 ? (a*te)%MOD : te);
}
void chmul(int &a, int b){ a = mul(a,b); }
void chmn(auto &a, auto b){ a = min(a, b); }
void chmx(auto &a, auto b){ a = max(a, b); }
int n, a[MAXN], b[MAXN], cnt[MAXN][3];
int ans;
signed 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]>>b[i];
if(a[i]<1) ans += 1-a[i], a[i] = 1;
if(a[i]>n) ans += a[i]-n, a[i] = n;
if(b[i]>2) ans += b[i]-2, b[i] = 2;
if(b[i]<1) ans += 1-b[i], b[i] = 1;
cnt[a[i]][b[i]]++;
}
// cout << ans << " ans\n";
set<pii>zer, isi; // salah satu
for(int i=1; i<=n; i++){
int tot = 0;
for(int j=1; j<=2; j++){
tot += cnt[i][j];
// cout << cnt[i][j] <<' ';
}
// cout << " p\n";
// harusnya 2;
// buang isi
int p = 0;
while(tot<2){
if(!isi.empty()){
ans += abs(i - (*isi.begin()).fi );
isi.erase(isi.begin());
} else {
zer.insert({i, ++p});
}
tot++;
}
while(tot > 2){ // buang zer
if(!zer.empty()){
ans += abs(i - (*zer.begin()).fi );
zer.erase(zer.begin());
} else {
isi.insert({i, ++p});
}
tot--;
}
}
int tot = 0;
for(int i=1; i<=n; i++){
tot += cnt[i][1];
}
ans += abs(n-tot);
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |