This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
int n, a[N][2];
ll ans = 0;
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < 2 * n; i++) {
int x, y;
cin >> x >> y;
if (x < 1) {
ans += 1 - x;
x = 1;
}
if (x > n) {
ans += x - n;
x = n;
}
if (y < 1) {
ans += 1 - y;
y = 1;
}
if (y > 2) {
ans += y - 2;
y = 2;
}
x--;
y--;
a[x][y]++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) a[i][j]--;
}
int s0 = 0, s1 = 0;
for (int i = 0; i < n; i++) {
s0 += a[i][0];
s1 += a[i][1];
if (s0 < 0 && s1 > 0) {
int delta = min(-s0, s1);
s0 += delta;
s1 -= delta;
ans += delta;
}
if (s0 > 0 && s1 < 0) {
int delta = min(s0, -s1);
s0 -= delta;
s1 += delta;
ans += delta;
}
ans += abs(s0) + abs(s1);
}
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |