제출 #1308937

#제출 시각아이디문제언어결과실행 시간메모리
1308937wangzhiyi33Coin Collecting (JOI19_ho_t4)C++20
100 / 100
34 ms2784 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define fir first
#define sec second

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    int cnt[n+1][3]; memset(cnt,0,sizeof cnt);
    int ans=0;

    for(int q=1;q<=2*n;q++){
        int x,y; cin>>x>>y;
        int barux=x,baruy=y;

        if(y<=1){
            if(x<1){
                barux=1;
            }
            else if(x>n){
                barux=n;
            }
            baruy=1;
        }
        else{
            if(x<1){
                barux=1;
            }
            else if(x>n){
                barux=n;
            }
            baruy=2;
        }
        ans+=abs(x-barux)+abs(y-baruy);
        cnt[barux][baruy]++;
    }

    int up=0,down=0;
    for(int q=1;q<=n;q++){
        up+=(cnt[q][1]-1); down+=(cnt[q][2]-1);
        if(up>0 && down<0){
            int brp=min(up,-down);
            up-=brp; down+=brp;
            ans+=brp;
        }
        if(down>0 && up<0){
            int brp=min(-up,down);
            up+=brp; down-=brp;
            ans+=brp;
        }

        ans+=abs(up)+abs(down);
    }
    cout<<ans<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...