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 ll long long
const int nx=2e5+5;
ll n, p[nx][2], dp[nx][2], res;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=2*n; i++)
{
cin>>p[i][0]>>p[i][1];
if (!(1<=p[i][1]&&p[i][1]<=2))
{
if (p[i][1]<1) res+=1-p[i][1], p[i][1]=1;
else res+=p[i][1]-2, p[i][1]=2;
}
if (!(1<=p[i][0]&&p[i][0]<=n))
{
if (p[i][0]<1) res+=1-p[i][0], p[i][0]=1;
else res+=p[i][0]-n, p[i][0]=n;
}
dp[p[i][0]][p[i][1]-1]++;
}
for (int i=1; i<=n; i++)
{
dp[i][0]+=dp[i-1][0];
res+=abs(dp[i-1][0]);
dp[i][1]+=dp[i-1][1];
res+=abs(dp[i-1][1]);
dp[i][0]--;
dp[i][1]--;
while (dp[i][0]<0&&dp[i][1]>0) dp[i][0]++, dp[i][1]--, res++;
while (dp[i][1]<0&&dp[i][0]>0) dp[i][1]++, dp[i][0]--, res++;
}
cout<<res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |