제출 #384805

#제출 시각아이디문제언어결과실행 시간메모리
384805valerikkCoin Collecting (JOI19_ho_t4)C++17
100 / 100
62 ms5868 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;

int n, a[N][2];
ll ans = 0;
 
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif 
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    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--;
        a[x][y]++;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 2; j++) a[i][j]--;
    }
    int s0 = 0, s1 = 0;
    for (int i = 0; i < n; i++) {
        s0 += a[i][0];
        s1 += a[i][1];
        
        if (s0 < 0 && s1 > 0) {
            int delta = min(-s0, s1);
            s0 += delta;
            s1 -= delta;
            ans += delta;
        }
        
        if (s0 > 0 && s1 < 0) {
            int delta = min(s0, -s1);
            s0 -= delta;
            s1 += delta;
            ans += delta;
        }
        ans += abs(s0) + abs(s1);
    }
    
    cout << ans << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...