#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
template<class T> bool ckmin(T& a, T b) { return b < a ? a = b, true : false; }
template<class T> bool ckmax(T& a, T b) { return a < b ? a = b, true : false; }
const int N = 1e5 + 7;
int a[N][3];
int main() {
ios::sync_with_stdio(0); cin.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;
}
++a[x][y];
}
for (int i = 1; i <= n; ++i) for (int j = 1; j <= 2; ++j) --a[i][j];
int s0 = 0, s1 = 0;
for (int i = 1; i <= n; ++i) {
s0 += a[i][1];
s1 += a[i][2];
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 << "\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... |