Submission #111462

#TimeUsernameProblemLanguageResultExecution timeMemory
111462oolimryCoin Collecting (JOI19_ho_t4)C++14
100 / 100
91 ms6556 KiB
#include <bits/stdc++.h>

using namespace std;
typedef pair<long long, long long> ii;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    long long ans = 0ll;
    int n;
    cin >> n;
    long long arr[n][2];

    for(int i = 0;i < n;i++){
        arr[i][0] = 0;
        arr[i][1] = 0;
    }

    for(int i = 0;i < 2 * n;i++){
        long long x, y;
        cin >> x >> y;
        if(x <= 1){
            ans += abs(x-1);
            x = 1;
        }
        else if(x >= n){
            ans += abs(x-n);
            x = n;
        }
        if(y <= 1){
            arr[x-1][0]++;
            ans += abs(y-1);
        }
        else{
            arr[x-1][1]++;
            ans += abs(y-2);
        }
    }

    for(int i = 0;i < n - 1;i++){
        long long x = arr[i][0];
        long long y = arr[i][1];

        long long ds[3] = {0ll, x - 1, 1 - y};
        ii best = ii(1203123214213,-1);
        for(int j = 0;j < 3;j++){
            best = min(best, ii(abs(ds[j]) + abs(x-ds[j]-1) + abs(y+ds[j]-1),ds[j]));
        }

        ans += best.first;
        arr[i+1][0] += (arr[i][0] - best.second - 1);
        arr[i+1][1] += (arr[i][1] + best.second - 1);
    }

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