#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define in insert
#define lb lower_bound
#define F first
#define S second
#define sz size()
#define all(v) v.begin(),v.end()
#define FOR1(x, n) for(int j = x; j <= n; j ++)
#define FOR(x, n, m, d) for(int x = n; x <= m; x += d)
#define FORR(x, n, m, d) for(int x = n; x >= m; x -= d)
#define nikita ios_base::sync_with_stdio(0), cin.tie(0);
const int N = 1e6+5;
int a[N], b[N], pref[N], dp[N], c[N][3];
int n,m,k,sum=0,x,y, ans, r, cnt, l, mod = 1e9+7, mt[N];
bool used[N];
vector<int>g[N];
string s, str;
map<int, int>mp;
void solve(){
nikita
cin >> n;
ans = 0;
k = 0;
FOR(i, 1, 2*n, 1){
cin >> b[i] >> a[i];
if( a[i] > 2 )ans += a[i] - 2, a[i] = 2;
else if(a[i] < 2)ans += abs(a[i] - 2), a[i] = 2, k ++;
if(b[i] > n)ans += b[i] - n, b[i] = n;
if(b[i] < 1)ans -= b[i] - 1, b[i] = 1;
c[b[i]][a[i]] ++;
}
FOR(i, 1, n, 1){
if( c[i][2] < 2 ){
FORR(j, i-1, 1, 1){
if(c[j][2]> 2){
c[j][2]--, c[i][2]++;
ans += (i-j);
if(c[i][2] == 2)break;
}
}
}
if(c[i][2] < 2){
FOR(j, i+1, n, 1){
if(c[j][2]> 2){
c[j][2]--, c[i][2]++;
ans += (j-i);
if(c[i][2] == 2)break;
}
}
}
}
cout << ans + n - min(n, k) * 2;
}
signed main(){
nikita
int tt = 1;
if(!tt)cin >> tt;
FOR(i, 1, tt, 1)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... |