Submission #570115

#TimeUsernameProblemLanguageResultExecution timeMemory
570115Rafi22Coin Collecting (JOI19_ho_t4)C++14
0 / 100
1 ms212 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
 
const int N=100007;
 
int a[N][2];
 
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,x,y;
    cin>>n;
    ll ans=0;
    for(int i=1;i<=2*n;i++)
    {
        cin>>x>>y;
        bool b;
        if(y>1)
        {
            ans+=y-2;
            b=1;
        }
        else
        {
            ans+=1-y;
            b=0;
        }
        if(x<1)
        {
            ans+=1-x;
            a[1][b]++;
        }
        else if(x>n)
        {
            ans+=x-n;
            a[n][b]++;
        }
        else
        {
            a[x][b]++;
        }
    }
    int j=0;
    int c0=0,c1=0;
    for(int i=1;i<=n;i++)
    {
        c0+=a[i][0];
        c1+=a[i][1];
        int d=i-j;
        if(c0+c1>=d*2)
        {
            int r0=max(0LL,c1-a[i][1]-d+1);
            c1-=r0;
            c0+=r0;
            int r1=max(0LL,c0-a[i][0]-d+1);
            c0-=r1;
            c1+=r1;
            int x=min(a[i][0],max(0LL,c0-d-max(0LL,d-c1)));
            a[i][0]-=x;
            a[i+1][0]+=x;
            ans+=x;
            x=min(a[i][1],max(0LL,c1-d-max(0LL,d-c0)));
            a[i][1]-=x;
            a[i+1][1]+=x;
            ans+=x;
            for(int l=i;l>j;l--)
            {
                if(a[l][0]==0)
                {
                    a[l][1]--;
                    a[l][0]++;
                    ans++;
                }
                if(a[l][1]==0)
                {
                    a[l][0]--;
                    a[l][1]++;
                    ans++;
                }
                a[l][0]--;
                a[l][1]--;
                ans+=a[l][0]+a[l][1];
                a[l-1][0]+=a[l][0];
                a[l-1][1]+=a[l][1];
                a[l][0]=0;
                a[l][1]=0;
            }
            j=i;
            c0=0;
            c1=0;
        }
    }
    cout<<ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...