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;
#define MAXN 100005
int n;
long long ans;
int arr[MAXN][2];
int main(){
// freopen("a.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 0; i<2*n; i++){
long long a, b; cin >> a >> b; --a; --b;
if(a < 0) ans += -a, a = 0;
if(a >= n) ans += a-n+1, a = n-1;
if(b > 1) ans += b-1, b = 1;
if(b < 0) ans += -b, b = 0;
arr[a][b]++;
}
int cur[2] = {0, 0};
for(int i = 0; i<n; i++){
cur[0] += arr[i][0]-1;
cur[1] += arr[i][1]-1;
for(int j = 0; j<2; j++){
if(cur[j] > 0 && cur[1-j] < 0){
int x = min(cur[j], -cur[1-j]);
cur[j] -= x, cur[1-j] += x, ans += x;
}
}
ans += abs(cur[0])+abs(cur[1]);
}
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... |