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;
const int N = 100000;
int coins[N][2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
long long ans = 0;
for (int i = 0; i < 2 * n; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
if (x < 0) {
ans += -x;
x = 0;
} else if (x >= n) {
ans += x - n + 1;
x = n - 1;
}
if (y <= 0) {
ans += -y;
++coins[x][0];
} else {
ans += y - 1;
++coins[x][1];
}
}
int k = 0;
for (int i = 0; i < n; ++i) {
while (coins[k][0] + coins[k][1] == 0) {
++k;
}
if (min(coins[k][0], coins[k][1]) > 0) {
ans += 2 * abs(k - i);
--coins[k][0], --coins[k][1];
} else {
int j = coins[k][0] > 0;
ans += abs(k - i);
--coins[k][1 - j];
while (coins[k][0] + coins[k][1] == 0) {
++k;
}
if (coins[k][j] > 0) {
ans += abs(k - i);
--coins[k][j];
} else {
ans += abs(k - i) + 1;
--coins[k][1 - j];
}
}
}
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... |