Submission #771840

#TimeUsernameProblemLanguageResultExecution timeMemory
7718401binCoin Collecting (JOI19_ho_t4)C++14
100 / 100
34 ms5068 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 5;
ll n, x, y, ans, cnt[NMAX][2], a, b, t;

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n;
    for(int i = 0; i < 2 * n; i++){
        cin >> x >> y;
        if(x < 1) ans += 1 - x, x = 1;
        else if(x > n) ans += x - n, x = n;
        if(y >= 2) ans += y - 2, y = 1;
        else ans += 1 - y, y = 0;
        cnt[x][y]++;
    }
    for(int i = 1; i <= n; i++){
        a += cnt[i][0] - 1;
        b += cnt[i][1] - 1;
        if(a * b < 0){
            if(a > 0){
                t = min(a, -b);
                a -= t; b += t; ans += t;
            }
            else{
                t = min(-a, b);
                b -= t, a += t, ans += t;
            }
        }
        ans += abs(a + b);
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...