#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n; cin >> n;
int ans = 0;
vector<array<int, 2>> db(n+1, {0, 0});
for(int i=0; i<2*n; i++){
int x, y; cin >> x >> y;
if(x < 1){
ans += 1 - x;
x = 1;
}else if(x > n){
ans += x - n;
x = n;
}
if(y < 1){
ans += 1 - y;
y = 1;
}else if(y > 2){
ans += y - 2;
y = 2;
}
db[x][y-1]++;
}
for(int i=1; i<=n; i++){
ans += abs(db[i-1][0]);
ans += abs(db[i-1][1]);
db[i][0] += db[i-1][0];
db[i][1] += db[i-1][1];
db[i][0]--;
db[i][1]--;
while(db[i][0] < 0 && db[i][1] > 0){
db[i][0]++;
db[i][1]--;
ans++;
}
while(db[i][0] > 0 && db[i][1] < 0){
db[i][0]--;
db[i][1]++;
ans++;
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |