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;
#define int long long
#define pii pair<long long, long long>
#define t3i tuple<long long, long long, long long>
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> x(2 * n), y(2 * n);
for (int i = 0; i < 2 * n; i++) cin >> x[i] >> y[i];
int ans = 0;
for (int i = 0; i < 2 * n; 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;
}
vector<vector<int>> q(n, vector<int>(2, -1));
for (int i = 0; i < 2 * n; i++) q[x[i] - 1][y[i] - 1]++;
for (int i = 0; i < n; i++) {
if (q[i][0] > 0 && q[i][1] < 0) {
int d = min(q[i][0], -q[i][1]);
ans += d;
q[i][0] -= d, q[i][1] += d;
}
if (q[i][0] < 0 && q[i][1] > 0) {
int d = min(-q[i][0], q[i][1]);
ans += d;
q[i][0] += d, q[i][1] -= d;
}
if (i < n - 1) {
q[i + 1][0] += q[i][0], q[i + 1][1] += q[i][1];
ans += abs(q[i][0]) + abs(q[i][1]);
q[i][0] = q[i][1] = 0;
}
}
/*for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
cout << q[j][i] << "\t";
}
cout << "\n";
}*/
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... |