Submission #558713

#TimeUsernameProblemLanguageResultExecution timeMemory
558713JomnoiCoin Collecting (JOI19_ho_t4)C++17
100 / 100
67 ms6092 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 1e5 + 5;

int cnt[3][MAX_N];

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int N;
    cin >> N;

    long long ans = 0;
    memset(cnt, -1, sizeof(cnt));
    for(int i = 1; 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;
        }

        cnt[y][x]++;
    }

    for(int i = 1; i <= N; i++) {
        int dif;
        if(cnt[1][i] < 0 and cnt[2][i] > 0) {
            dif = min(-cnt[1][i], cnt[2][i]);
            ans += dif;
            cnt[1][i] += dif;
            cnt[2][i] -= dif;
        }
        if(cnt[1][i] > 0 and cnt[2][i] < 0) {
            dif = min(cnt[1][i], -cnt[2][i]);
            ans += dif;
            cnt[1][i] -= dif;
            cnt[2][i] += dif;
        }

        ans += abs(cnt[1][i]) + abs(cnt[2][i]);

        cnt[1][i + 1] += cnt[1][i];
        cnt[2][i + 1] += cnt[2][i];
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...