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 all(i) (i).begin(), (i).end()
using namespace std;
template<typename T1, typename ...T2>
void debug(bool _split, T1 i, T2 ...j) {
if (_split)
cerr << ", ";
cerr << i;
debug(true, j...);
}
#define debug(args...) cout << "Line" << __LINE__ << " : [" << #args << << "] is [" << debug(false, args) << "]" << endl;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = 0x3f3f3f3f, lg = 20;
const ll mod = 1e9 + 7, INF = 0x3f3f3f3f;
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
ll ans = 0;
vector<vector<int>> a(n + 1, vector<int>(2, -1));
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) {
if (a[i][0] > 0 && a[i][1] < 0) {
int x = min(a[i][0], -a[i][1]);
a[i][0] -= x, a[i][1] += x;
ans += x;
}
else if (a[i][0] < 0 && a[i][1] > 0) {
int x = min(-a[i][0], a[i][1]);
a[i][0] += x, a[i][1] -= x;
ans += x;
}
a[i + 1][0] += a[i][0], a[i + 1][1] += a[i][1];
ans += abs(a[i][0]) + abs(a[i][1]);
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |