#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;
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]]++;
}
vector<int> d[3], s[3];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2; j++) {
if (f[i][j]) {
for (int k = 1; k < f[i][j]; k++) {
if (d[j].empty() && d[3-j].empty()) s[j].push_back(i);
else if (!d[j].empty()) {
ans += abs(i - d[j].back());
d[j].pop_back();
} else {
ans += abs(i - d[3-j].back()) + 1;
d[3-j].pop_back();
}
}
} else {
if (s[j].empty() && s[3-j].empty()) d[j].push_back(i);
else if (!s[j].empty()) {
ans += abs(i - s[j].back());
s[j].pop_back();
} else {
ans += abs(i - s[3-j].back()) + 1;
s[3-j].pop_back();
}
}
}
}
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... |