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;
typedef long long lint;
typedef pair<int, int> pii;
const int INF = 2e9;
const int MOD = 1e9+7;
const int MAX = 1e5+10;
const lint LNF = 2e18;
int n, A[2][MAX];
lint ans;
void go(int &x, int z){
while(x<=n && A[z][x]==0) x++;
if(x>n) x = INF;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n;
for(int i=1; i<=2*n; i++){
int x,y; cin>>x>>y;
ans += min(abs(y-1), abs(y-2));
ans += (1<=x && x<=n ? 0 : min(abs(x-1), abs(x-n)));
y = (y<=1 ? 1 : 2) - 1;
x = (1<=x && x<=n ? x : (x<=1 ? 1 : n));
A[y][x]++;
}
// cout<<ans<<'\n';
// for(int i=1; i<=n; i++) cout<<A[0][i]<<' ';
// cout<<'\n';
// for(int i=1; i<=n; i++) cout<<A[1][i]<<' ';
// cout<<'\n';
for(int i=1; i<=n; i++){
static int p[2]={1,1};
go(p[0],0); go(p[1],1);
if(p[0]==p[1]){
ans += abs(p[0]-i)*2;
A[0][p[0]]--, A[1][p[1]]--;
}
else{
int z = (p[0]<p[1] ? 0 : 1);
A[z][p[z]]--; ans += abs(p[z]-i);
go(p[z], z);
int w = (p[0]==p[1] ? 1-z : (p[0]<p[1] ? 0 : 1));
A[w][p[w]]--; ans += abs(p[w]-i);
if(w==z) ans++;
}
}
cout<<ans<<'\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... |