Submission #1296103

#TimeUsernameProblemLanguageResultExecution timeMemory
1296103fairkrashCoin Collecting (JOI19_ho_t4)C++20
100 / 100
53 ms6624 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
ll INF = 1e18;
ll n;
vector<pair<ll, ll>> s;
vector<vector<ll>> pref;

ll an(ll x, ll y) {
    ll ans = 0;
    pref.assign(2, vector<ll>(n, 0));
    for (ll i = 0; i < s.size(); i++) {
        if (s[i].first < x) {
            if (s[i].second <= y) {
                ans += abs(x - s[i].first) + abs(y - s[i].second);
                pref[1][0]++;
                continue;
            } else {
                ans += abs(x - s[i].first) + abs((y + 1) - s[i].second);
                pref[0][0]++;
                continue;
            }
        }
        if (s[i].first > x + n - 1) {
            if (s[i].second <= y) {
                ans += abs((x + n - 1) - s[i].first) + abs(y - s[i].second);
                pref[1][n - 1]++;
                continue;
            } else {
                ans += abs((x + n - 1) - s[i].first) + abs((y + 1) - s[i].second);
                pref[0][n - 1]++;
                continue;
            }
        }
        if (y + 1 <= s[i].second) {
            ans += abs((y + 1) - s[i].second);
            pref[0][s[i].first - x]++;
        } else {
            ans += abs((y) - s[i].second);
            pref[1][s[i].first - x]++;
        }
    }
    deque<ll> d1, d2;
    deque<ll> cool1, cool2;
    for (ll j = 0; j < n; j++) {
        if (pref[0][j] == 0) {
            d1.push_back(j);
        }
        if (pref[1][j] == 0) {
            d2.push_back(j);
        }
        if (!cool1.empty() && !d1.empty()) {
            ans += abs(j - cool1.front());
            cool1.pop_front();
            d1.pop_front();
        }
        if (!cool2.empty() && !d2.empty()) {
            ans += abs(j - cool2.front());
            cool2.pop_front();
            d2.pop_front();
        }
        if (!cool1.empty() && !d2.empty()) {
            ans += abs(j - cool1.front()) + 1;
            cool1.pop_front();
            d2.pop_front();
        }
        if (!cool2.empty() && !d1.empty()) {
            ans += abs(j - cool2.front()) + 1;
            cool2.pop_front();
            d1.pop_front();
        }
        while (!d1.empty() && pref[0][j] > 1) {
            ans += j - d1.front();
            d1.pop_front();
            pref[0][j]--;
        }
        if (d1.empty() && pref[0][j] > 1) {
            for (ll e = 1; e < pref[0][j]; e++) {
                cool1.push_back(j);
            }
        }
        while (!d2.empty() && pref[1][j] > 1) {
            ans += j - d2.front();
            d2.pop_front();
            pref[1][j]--;
        }
        if (d2.empty() && pref[1][j] > 1) {
            for (ll e = 1; e < pref[1][j]; e++) {
                cool2.push_back(j);
            }
        }
        while (!cool1.empty() && !d2.empty()) {
            ans += abs(d2.front() - cool1.front()) + 1;
            cool1.pop_front();
            d2.pop_front();
        }
        while (!cool2.empty() && !d1.empty()) {
            ans += abs(d1.front() - cool2.front()) + 1;
            cool2.pop_front();
            d1.pop_front();
        }
    }
    return ans;
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n;
    s.assign(n * 2, {});
    for (ll i = 0; i < n * 2; i++) {
        cin >> s[i].first >> s[i].second;
    }
    cout << an(1, 1);
}
// 3
// 0 0
// 0 4
// 4 0
// 2 1
// 2 5
// -1 1
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...