제출 #723492

#제출 시각아이디문제언어결과실행 시간메모리
723492PringCoin Collecting (JOI19_ho_t4)C++14
100 / 100
63 ms9676 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int, int> pii;

const int MXN = 200005;
int N, n, ans, c[MXN][2];
pii a[MXN];

int DIS(pii &a, pii &b) {
    return abs(a.first - b.first) + abs(a.second - b.second);
}

void PUT(int id) {
    pii tr;
    tr.second = (a[id].second >= 2 ? 2 : 1);
    if (1 <= a[id].first && a[id].first <= n) tr.first = a[id].first;
    else tr.first = (a[id].first < 1 ? 1 : n);
    c[tr.first][tr.second - 1]++;
    ans += DIS(tr, a[id]);
}

void SOLVE() {
    // int up = 0, down = 0;
    // for (int i = 1; i <= n; i++) {
    //     up += c[i][1];
    //     down += c[i][0];
    // }
    // ans += abs(n - up);
    // int left = 0, right = N;
    // for (int i = 1; i < n; i++) {
    //     left += (c[i][0] + c[i][1]);
    //     right -= (c[i][0] + c[i][1]);
    //     ans += abs(i * 2 - left);
    // }
    for (int i = 1; i <= n; i++) {
        c[i][0]--;
        c[i][1]--;
        // cout << c[i][0] << ' ' << c[i][1] << endl;
        if ((c[i][0] > 0) ^ (c[i][1] > 0)) {
            if (abs(c[i][0]) > abs(c[i][1])) {
                ans += abs(c[i][1]);
                c[i][0] = c[i][0] / abs(c[i][0]) * abs(c[i][0] + c[i][1]);
                c[i][1] = 0;
            } else {
                ans += abs(c[i][0]);
                c[i][1] = c[i][1] / abs(c[i][1]) * abs(c[i][1] + c[i][0]);
                c[i][0] = 0;
            }
        }
        ans += abs(c[i][0]) + abs(c[i][1]);
        // cout << c[i][0] << ' ' << c[i][1] << endl;
        c[i + 1][0] += c[i][0];
        c[i + 1][1] += c[i][1];
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    N = n * 2;
    for (int i = 0; i < N; i++) {
        cin >> a[i].first >> a[i].second;
        PUT(i);
    }
    // for (int j = 1; j >= 0; j--) {
        // for (int i = 1; i <= n; i++) {
        //     cout << c[i][j] << ' ';
        // }
        // cout << endl;
    // }
    SOLVE();
    cout << ans << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...