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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 2e9 + 2;
int cnt[N][3];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin >> n;
ll ans = 0;
for (int i = 1; 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;
}
cnt[x][y] += 1;
}
ll u, d;
u = d = 0;
for (int i = 1; i <= n; i++) {
u += cnt[i][1] - 1;
d += cnt[i][2] - 1;
if (u < 0 && d > 0) {
ll x = min(-u, d);
ans += x;
u += x;
d -= x;
}
if (u > 0 && d < 0) {
ll x = min(u, -d);
ans += x;
u -= x;
d += x;
}
ans += abs(u) + abs(d);
}
cout << ans << en;
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... |