Submission #230010

#TimeUsernameProblemLanguageResultExecution timeMemory
230010Andrei_CotorCoin Collecting (JOI19_ho_t4)C++11
100 / 100
71 ms5880 KiB
#include<iostream>
#include<deque>

using namespace std;

int Nr[2][200005];
deque<int> Q[2];
int Ind[2];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin>>n;

    long long rez=0;
    for(int i=1; i<=2*n; i++)
    {
        int x,y;
        cin>>x>>y;

        if(x<1)
        {
            rez+=1LL*(1-x);
            x=1;
        }

        if(x>n)
        {
            rez+=1LL*(x-n);
            x=n;
        }

        if(y<1)
        {
            rez+=1LL*(1-y);
            y=1;
        }

        if(y>2)
        {
            rez+=1LL*(y-2);
            y=2;
        }

        Nr[y-1][x]++;
    }

    for(int i=1; i<=n; i++)
    {
        for(int k=0; k<2; k++)
        {
            for(int j=1; j<=Nr[k][i]; j++)
                Q[k].push_back(i);
        }

        for(int k=0; k<2; k++)
        {
            while(Ind[k]<i && !Q[k].empty())
            {
                Ind[k]++;
                rez+=abs(Ind[k]-Q[k].front());
                Q[k].pop_front();
            }
        }

        for(int k=0; k<2; k++)
        {
            while(Ind[k]<i && !Q[1-k].empty())
            {
                Ind[k]++;
                rez+=abs(Ind[k]-Q[1-k].front())+1;
                Q[1-k].pop_front();
            }
        }
    }

    cout<<rez<<"\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...