#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false
const int MAXN = 200 * 1000;
pii a[MAXN];
int ile[MAXN][3];
int main () {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < 2 * n; i ++) {
cin >> a[i].st >> a[i].nd;
}
ll wyn = 0;
for (int i = 0; i < 2 * n; i ++) {
if (abs(a[i].nd - 1) < abs(a[i].nd - 2)) {
wyn += ll(abs(a[i].nd - 1));
if (1 <= a[i].st && a[i].st <= n) {
ile[a[i].st][1] ++;
} else {
if (a[i].st < 1) {
ile[1][1] ++;
wyn += ll(abs(a[i].st - 1));
} else {
ile[n][1] ++;
wyn += ll(abs(a[i].st - n));
}
}
} else {
wyn += ll(abs(a[i].nd - 2));
if (1 <= a[i].st && a[i].st <= n) {
ile[a[i].st][2] ++;
} else {
if (a[i].st < 1) {
ile[1][2] ++;
wyn += ll(abs(a[i].st - 1));
} else {
ile[n][2] ++;
wyn += ll(abs(a[i].st - n));
}
}
}
}
for (int i = 1; i <= n; i ++) {
ile[i][1] --;
ile[i][2] --;
//cout << ile[i][1] << " " << ile[i][2] << "\n";
}
for (int i = 1; i <= n; i ++) {
if (ile[i][1] < 0) {
if (ile[i][2] >= 1) {
ll x = min(ile[i][2], -ile[i][1]);
ile[i][2] -= x;
ile[i][1] += x;
wyn += x;
}
} else if (ile[i][2] < 0) {
if (ile[i][1] >= 1) {
ll x = min(-ile[i][2], ile[i][1]);
ile[i][1] -= x;
ile[i][2] += x;
wyn += x;
}
}
wyn += abs(ile[i][1]) + abs(ile[i][2]);
ile[i + 1][1] += ile[i][1];
ile[i + 1][2] += ile[i][2];
}
cout << wyn << "\n";
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... |