#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 1e5 + 10;
int x[2 * N], y[2 * N];
int f[N][3];
int ans = 0;
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
vector<pair<int, int>> v;
int cnt = 0;
for (int i = 1; i <= 2 * n; i++) {
cin >> x[i] >> y[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;
}
f[x[i]][y[i]]++;
if (y[i] == 1) cnt++;
}
ans += abs(n - cnt);
vector<int> d, s;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2; j++) {
if (!f[i][j]) d.push_back(i);
else {
int delta = f[i][j] - 1;
while (delta--) s.push_back(i);
}
}
}
for (int i = 0; i < d.size(); i++) ans += abs(d[i] - s[i]);
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... |