#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back
using namespace std;
ll n, x[200000], y[200000];
int main() {
cin >> n;
for (ll i = 0; i < 2 * n; i++) cin >> x[i] >> y[i];
ll ans = 0, c[n + 1][3] = {};
for (ll i = 0; i < 2 * n; i++) {
if (x[i] < 1) ans += 1 - x[i], x[i] = 1;
if (x[i] > n) ans += x[i] - n, x[i] = n;
if (y[i] < 1) ans += 1 - y[i], y[i] = 1;
if (y[i] > 2) ans += y[i] - 2, y[i] = 2;
c[x[i]][y[i]]++;
}
ll b[3] = {};
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= 2; j++) b[j] += c[i][j] - 1;
for (ll j = 1; j <= 2; j++) if (b[j] > 0 && b[3 - j] < 0) {
ll t = min(b[j], -b[3 - j]);
ans += t, b[j] -= t, b[3 - j] += t;
}
for (ll j = 1; j <= 2; j++) ans += abs(b[j]);
}
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... |