제출 #872979

#제출 시각아이디문제언어결과실행 시간메모리
872979LeVanThucCoin Collecting (JOI19_ho_t4)C++17
100 / 100
37 ms7216 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,T2 y)
{
    if(x<y)
    {
        x=y;
        return 1;
    }
    return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,T2 y)
{
    if(y<x)
    {
        x=y;
        return 1;
    }
    return 0;
}
void online()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef thuc
    freopen("input.INP","r",stdin);
    freopen("output.OUT","w",stdout);
#else

#endif
}
const ll Mihacutephomaique=1e5+10;
priority_queue<pll> miha1;
ll n,k;
ll a[4][Mihacutephomaique];
int main()
{
    online();
    cin>>n;
    ll lv=0,mh=0,ans=0;
    for(int i=1;i<=2*n;i++)
    {
        ll x,y;
        cin>>y>>x;
        if(x<1)
        {
            ans+=1-x;
            x=1;
        }
        if(x>1)
        {
            ans+=x-2;
            x=2;
        }
        if(y>n)
        {
            ans+=y-n;
            y=n;
        }
        if(y<1)
        {
            ans+=1-y;
            y=1;
        }
        a[x][y]++;
    }
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=n;j++)
        {
            a[i][j]--;
        }
    }
    for(int j=1;j<=n;j++)
    {
        lv+=a[1][j];
        mh+=a[2][j];
        if((lv<0&&mh>0)||(lv>0&&mh<0))
        {
            if(abs(lv)<abs(mh))
            {
                mh+=lv;
                ans+=abs(lv);
                lv=0;
            }
            else
            {
                lv+=mh;
                ans+=abs(mh);
                mh=0;
            }
        }
        ans+=abs(lv)+abs(mh);
    }
    cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...