Submission #813543

#TimeUsernameProblemLanguageResultExecution timeMemory
813543Charizard2021Coin Collecting (JOI19_ho_t4)C++17
100 / 100
128 ms6544 KiB
#include <bits/stdc++.h>
using namespace std;
const long long N = 200001;
long long dp[2][N];
int main(){
    long long n;
    cin >> n;
    long long ans = 0;
    for(long long i = 0; i < 2 * n; i++){
        long long 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;
        }
        dp[y - 1][x]++;
    }
    for(long long i = 1; i <= n; i++){
        dp[0][i] -= 1;
        dp[1][i] -= 1;
        if(dp[0][i] * dp[1][i] < 0){
            if(dp[0][i] < 0){
                long long x = min(abs(dp[0][i]), dp[1][i]);
                dp[0][i] += x;
                dp[1][i] -= x;
                ans += x;
            }
            else{
                long long x = min(abs(dp[1][i]), dp[0][i]);
                dp[0][i] -= x;
                dp[1][i] += x;
                ans += x;
            }
        }
        ans += abs(dp[0][i]) + abs(dp[1][i]);
        dp[0][i + 1] += dp[0][i];
        dp[1][i + 1] += dp[1][i];
    }
    cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...