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 int long long
#define pii pair<int, int>
signed main(){
int n;
cin>>n;
vector<pair<int, int>> v(2*n);
for(int i = 0; i<2*n; i++){
cin>>v[i].first>>v[i].second;
v[i].first--;v[i].second--;
}
vector<vector<int>> v2(2, vector<int> (n, -1));
int res= 0;
for(int i = 0; i<2*n; i++){
auto cur = v[i];
if(cur.first<0){
res += -cur.first;
cur.first = 0;
}
else if(cur.first>=n){
res += cur.first -(n-1);
cur.first = n-1;
}
if(cur.second<0){
res+=-cur.second;
cur.second = 0;
}
else if(cur.second>1){
res += cur.second -1;
cur.second = 1;
}
v2[cur.second][cur.first] ++;
}
//cout<<res<<endl;
vector<int> balance(2, 0);
for(int i = 0; i<n; i++){
for(int side= 0; side<2; side++){
balance[side] += v2[side][i];
}
if(balance[0]*balance[1]<0){
int transfer = min(abs(balance[0]), abs(balance[1]));
if(balance[0]>0){
balance[0]-= transfer;
balance[1] += transfer;
}
else{
balance[0]+= transfer;
balance[1] -= transfer;
}
res+=transfer;
}
for(int side = 0; side<2; side++){
res+=abs(balance[side]);
}
}
cout<<res<<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... |