#include <iostream>
#include <queue>
using namespace std;
long long n, x, y, xx, yy, a[100010][3], ans;
queue<int> q[3];
int main() {
cin >> n;
for (int i = 1; i <= 2*n; i++) {
cin >> x >> y;
if (y <= 1) yy = 1;
else yy = 2;
if (x < 1) xx = 1;
else if (x > n) xx = n;
else xx = x;
a[xx][yy]++;
ans += abs(x-xx)+abs(y-yy);
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= 2; j++) for (int k = 2; k <= a[i][j]; k++) q[j].push(i);
for (int i = 1; i <= n; i++) {
if (a[i][1] == 0) {
if ((q[2].empty()) || ((q[1].size()) && (abs(i-q[1].front()) <= abs(i-q[2].front())+1))) {
ans += abs(i-q[1].front());
q[1].pop();
} else {
ans += abs(i-q[2].front())+1;
q[2].pop();
}
}
if (a[i][2] == 0) {
if ((q[1].empty()) || ((q[2].size()) && (abs(i-q[2].front()) <= abs(i-q[1].front())+1))) {
ans += abs(i-q[2].front());
q[2].pop();
} else {
ans += abs(i-q[1].front())+1;
q[1].pop();
}
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |