This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//я так много думал, что опять попал
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define f first
#define s second
#define left(v) v + v
#define right(v) v + v + 1
#define ub upper_bound
#define lb lower_bound
#define pll pair<ll,ll>
using namespace std;
typedef long long ll;
//17 SEVENTEEN
const long double Pi = acos(-1.0);
const ll dx[] = {0,0,1,-1};
const ll dy[] = {1,-1,0,0};
const ll N = (ll) 1e6 + 17;
const ll M = (ll) 5e3 + 69;
const ll inf = (ll) 1e14 + 3;
const ll mod = (ll) 1e9 + 7;
ll sq(ll x) { return x * x; }
ll zxc = 1, x, y, a[3][N], sum[5];
void solve() {
ll n, ans = 0;
cin >> n;
for(ll i = 1; i <= 2 * n; i++) {
cin >> y >> x;
if(1 <= x && x <= 2 && 1 <= y && y <= n) a[x][y]++;
else if(x < 1 && y < 1) {
a[1][1]++;
ans += abs(x - 1), ans += abs(y - 1);
}
else if(x < 1 && y > n) {
a[1][n]++;
ans += abs(x - 1), ans += abs(y - n);
}
else if(y < 1 && x > 2) {
a[2][1]++;
ans += abs(x - 2), ans += abs(y - 1);
}
else if(x > 2 && y > n) {
a[2][n]++;
ans += abs(x - 2), ans += abs(y - n);
}
else if(1 <= x && x <= 2 && y < 1)
a[x][1]++, ans += abs(y - 1);
else if(1 <= x && x <= 2 && y > n)
a[x][n]++, ans += abs(y - n);
else if(1 <= y && y <= n && x < 1)
a[1][y]++, ans += abs(x - 1);
else if(1 <= y && y <= n && x > 2)
a[2][y]++, ans += abs(x - 2);
}
// for(ll i = 1; i <= 2; i++) {
// for(ll j = 1; j <= n; j++) {
// cout << a[i][j] << " ";
// sum[i] += a[i][j];
// }
// cout << sum[i] << "\n";
// }
if(sum[1] > sum[2]) {
ll x = a[1][1] - 1;
a[1][1] -= min(x, sum[1] - sum[2]);
a[2][1] += min(x, sum[1] - sum[2]);
sum[1] -= min(x, sum[1] - sum[2]);
ans += min(x, sum[1] - sum[2]);
x = a[1][n] - 1;
a[1][n] -= min(x, sum[1] - sum[2]);
a[2][n] += min(x, sum[1] - sum[2]);
ans += min(x, sum[1] - sum[2]);
}
else {
ll x = a[2][1] - 1;
a[2][1] -= min(x, sum[2] - sum[1]);
a[1][1] += min(x, sum[2] - sum[1]);
sum[2] -= min(x, sum[2] - sum[1]);
ans += min(x, sum[2] - sum[1]);
x = a[1][n] - 1;
a[2][n] -= min(x, sum[2] - sum[1]);
a[1][n] += min(x, sum[1] - sum[2]);
sum[2] -= min(x, sum[2] - sum[1]);
ans += min(x, sum[2] - sum[1]);
}
for(ll i = 1; i <= 2; i++) {
for(ll j = 1; j <= n; j++) {
// cout << a[i][j] << " ";
sum[i] += a[i][j];
}
// cout << "\n";
}
for(ll i = 1; i <= 2; i++) {
for(ll j = 1; j <= n; j++) {
if(a[i][j] > 1) a[i][j]--, a[i][j + 1]++, ans++;
else break;
}
for(ll j = n; j >= 1; j--) {
if(a[i][j] > 1) a[i][j]--, a[i][j - 1]++, ans++;
else break;
}
}
// vector<pair<ll, pll> > v;
// v.pb({a[1][1], {1, 1}});
// v.pb({a[1][n], {1, n}});
// v.pb({a[2][1], {2, 1}});
// v.pb({a[2][n], {2, n}});
//
// sort(all(v));
// reverse(all(v));
//
// for(auto cur : v) {
// queue<pll> q;
// q.push({cur.s.f, cur.s.s});
// ll cnt = cur.f;
// while(cnt) {
// ll x = q.front().f, y = q.front().s;
// q.pop();
// for(ll i = 0; i < 4; i++) {
// ll qx = x + dx[i], qy = y + dy[i];
// if(qx < 1 || qy < 1 || qx > 2 || qy > n || a[qx][qy]) continue;
//
// if(cnt)
// a[qx][qy] = 1, q.push({qx, qy}), cnt--, ans += abs(cur.s.f - qx) + abs(cur.s.s - qy);
// }
// }
// while(!q.empty()) q.pop();
// }
cout << ans;
}
int main(/*Уверенно*/) {
ios_base::sync_with_stdio(0);
cin.tie(0);
/*
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
*/
// cin >> zxc;
while(zxc--) {
solve();
}
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... |