제출 #1347549

#제출 시각아이디문제언어결과실행 시간메모리
1347549ChottuFCoin Collecting (JOI19_ho_t4)C++20
100 / 100
94 ms2628 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
    int n;
    cin >> n;
    vector<vector<int>> dp(2, vector<int>(n,-1));
    int ans = 0;
    for (int i = 0; i<2*n; i++){
        int x,y;
        cin >> x >> y;
        if (x <= 1){
            ans += 1-x;
            x = 1;
        }
        if (x >= n){
            ans += x-n;
            x = n;
        }
        if (y <= 1){
            ans += 1-y;
            y = 1;
        }
        if (y >= 2){
            ans += y-2;
            y = 2;
        }
        x--;y--; //0-index lol
        dp[y][x]++;
    }
    for (int i = 0; i<n; i++){
        int one = dp[0][i];
        int two = dp[1][i];
        
        if (one >= 0 && two <= 0){
            int df = min(abs(one), abs(two));
            dp[0][i] -= df;
            dp[1][i] += df;
            ans += df;
        }
        else if (one <= 0 && two >= 0){
            int df = min(abs(one), abs(two));
            dp[0][i] += df;
            dp[1][i] -= df;
            ans += df;
        }

        if (i+1 < n){
            dp[0][i+1] += dp[0][i];
            dp[1][i+1] += dp[1][i];
            ans += abs(dp[0][i]) + abs(dp[1][i]);
        }
    }
    cout << ans;
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...