Submission #1296093

#TimeUsernameProblemLanguageResultExecution timeMemory
1296093fairkrashCoin Collecting (JOI19_ho_t4)C++20
0 / 100
1 ms572 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]++;
        }
    }
    vector<pair<ll, ll>> d1, d2;
    for (ll i = 0; i < 2; i++) {
        for (ll j = 0; j < n; j++) {
            for (ll e = 1; e < pref[i][j]; e++) {
                d1.push_back({j, i});
            }
        }
    }
    for (ll i = 0; i < 2; i++) {
        for (ll j = 0; j < n; j++) {
            if (pref[i][j] == 0) {
                d2.push_back({j, i});
            }
        }
    }
    if (d1.empty())return ans;
    std::sort(d1.begin(), d1.end());
    std::sort(d2.begin(), d2.end());
    for (ll i = 0; i < d1.size(); i++) {
        ans += abs(d1[i].first - d2[i].first) + abs(d1[i].second - d2[i].second);
    }
    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...