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>
#define endl '\n'
using namespace std;
const int MAXN=1e5+5;
int n;
int cnt[2][MAXN];
long long ans=0;
void add(int t,int x)
{
if(x<1)
{
ans+=1-x;
cnt[t][1]++;
}
else if(x>n)
{
ans+=x-n;
cnt[t][n]++;
}
else cnt[t][x]++;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=1;i<=2*n;i++)
{
int x,y;
cin>>x>>y;
if(y<=1)
{
add(0,x);
ans+=1-y;
}
else
{
add(1,x);
ans+=y-2;
}
}
int r0=0,r1=0;
for(int i=1;i<=n;i++)
{
r0+=cnt[0][i];
r1+=cnt[1][i];
int vert=0;
if(r0<i && i<r1)
{
vert=min(i-r0, r1-i);
r0+=vert;
r1-=vert;
}
if(r0>i && i>r1)
{
vert=min(r0-i, i-r1);
r0-=vert;
r1+=vert;
}
ans+=vert;
ans+=abs(r0+r1-2*i);
}
cout<<ans<<endl;
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... |