Submission #1150750

#TimeUsernameProblemLanguageResultExecution timeMemory
1150750koukirocksCoin Collecting (JOI19_ho_t4)C++20
100 / 100
34 ms2784 KiB
#include<bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) x.begin()+1, x.end()
#define F first
#define S second

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll cnt[100010][3];

int main() {
    speed;
    ll n;
    cin>>n;
    ll ans=0;
    memset(cnt,0,sizeof(cnt));
    for (ll i=1;i<=2*n;i++) {
        ll x,y;
        cin>>x>>y;
        if (x<=1) {
            if (y<=1) {
                ans+=(1-x)+(1-y);
                cnt[1][1]++;
            } else {
                cnt[1][2]++;
                ans+=(1-x)+(y-2);
            }
        } else if (n<=x) {
            if (y<=1) {
                cnt[n][1]++;
                ans+=(x-n)+(1-y);
            } else {
                cnt[n][2]++;
                ans+=(x-n)+(y-2);
            }
        } else {
            if (y<=1) {
                cnt[x][1]++;
                ans+=(1-y);
            } else {
                cnt[x][2]++;
                ans+=(y-2);
            }
        }
    }
    ll d1=0,d2=0;
    for (int i=1;i<=n;i++) {
        d1+=cnt[i][1]-1;
        d2+=cnt[i][2]-1;
        // cout<<d1<<" "<<d2<<" 1\n";
        if (d1<0 and 0<d2) {
            ll t=min(-d1,d2);
            ans+=t;
            d1+=t;
            d2-=t;
        } else if (d2<0 and 0<d1) {
            ll t=min(d1,-d2);
            ans+=t;
            d1-=t;
            d2+=t;
        }
        ans+=abs(d1)+abs(d2);
        // cout<<d1<<" "<<d2<<" 2\n\n";
    }
    cout<<ans<<"\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...