#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int MXN = 200005;
int N, n, ans, c[MXN][2];
pii a[MXN];
int DIS(pii &a, pii &b) {
return abs(a.first - b.first) + abs(a.second - b.second);
}
void PUT(int id) {
pii tr;
tr.second = (a[id].second >= 2 ? 2 : 1);
if (1 <= a[id].first && a[id].first <= n) tr.first = a[id].first;
else tr.first = (a[id].first < 1 ? 1 : n);
c[tr.first][tr.second - 1]++;
ans += DIS(tr, a[id]);
}
void SOLVE() {
// int up = 0, down = 0;
// for (int i = 1; i <= n; i++) {
// up += c[i][1];
// down += c[i][0];
// }
// ans += abs(n - up);
// int left = 0, right = N;
// for (int i = 1; i < n; i++) {
// left += (c[i][0] + c[i][1]);
// right -= (c[i][0] + c[i][1]);
// ans += abs(i * 2 - left);
// }
for (int i = 1; i <= n; i++) {
c[i][0]--;
c[i][1]--;
if ((c[i][0] > 0) ^ (c[i][1] > 0)) {
if (abs(c[i][0]) > abs(c[i][1])) {
ans += abs(c[i][1]);
c[i][0] = c[i][0] / abs(c[i][0]) * abs(c[i][0]) - abs(c[i][1]);
c[i][1] = 0;
} else {
ans += abs(c[i][0]);
c[i][1] = c[i][1] / abs(c[i][1]) * abs(c[i][1]) - abs(c[i][0]);
c[i][0] = 0;
}
}
ans += abs(c[i][0]) + abs(c[i][1]);
c[i + 1][0] += c[i][0];
c[i + 1][1] += c[i][1];
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
N = n * 2;
for (int i = 0; i < N; i++) {
cin >> a[i].first >> a[i].second;
PUT(i);
}
// for (int j = 1; j >= 0; j--) {
// for (int i = 1; i <= n; i++) {
// cout << c[i][j] << ' ';
// }
// cout << endl;
// }
SOLVE();
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |