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 pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
#define int long long
const int N = (int)2e5+10;
int n, a[N][3], b[2];
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n; int ans = 0, xd = 0;
for(int i = 0; i < 2*n; i++){
int x, y; cin >> x >> y;
int X = x, Y = y;
x=clamp(x,1ll,n), y=clamp(y,1ll,2ll);
ans+=abs(X-x)+abs(Y-y); a[x][y]++;
}
for(int i = 1; i <= n; i++){
b[0]+=a[i][1]-1,b[1]+=a[i][2]-1;
xd=min(abs(b[0]),abs(b[1]));
if(b[0]>0 and b[1]<0) ans+=xd,b[0]-=xd,b[1]+=xd;
else if(b[1]>0 and b[0]<0) ans+=xd,b[1]-=xd,b[0]-=xd;
ans+=abs(b[0])+abs(b[1]);
}
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... |