제출 #1361275

#제출 시각아이디문제언어결과실행 시간메모리
1361275gayCoin Collecting (JOI19_ho_t4)C++20
0 / 100
0 ms344 KiB
#include <bits/stdc++.h>

using namespace std;

using ld = long double;
using ll = long long;

const ll INF = 3e18, MOD = 1e9 + 7;

void solve();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q = 1;
    while (q--) {
        solve();
    }
}

void solve() {
    ll n;
    cin >> n;
    vector<pair<ll, ll>> a(2 * n);
    for (int i = 0; i < 2 * n; i++) {
        cin >> a[i].first >> a[i].second;
    }
    sort(a.begin(), a.end());
    vector<ll> z(2 * n);
    ll ans = 0;
    for (ll i = 1; i < 2 * n; i += 2) {
        if (a[i - 1].second > a[i].second) {
            swap(a[i - 1], a[i]);
        }
        ll cur = (i + 1) / 2;
        ans += abs(cur - a[i - 1].first) + abs(cur - a[i].first);
        z[i - 1] = 1;
        z[i] = 2;
    }
    ll ans2 = ans;
    ll l = 0;
    while (l < 2 * n) {
        ll r = l;
        while (r < 2 * n && a[r].first == a[l].first) {
            r++;
        }
        vector<ll> c1, c2;
        for (ll i = l; i < r; i++) {
            c1.push_back(z[i]);
            c2.push_back(a[i].second);
        }
        sort(c1.begin(), c1.end());
        sort(c2.begin(), c2.end());
        for (ll i = 0; i < c1.size(); i++) {
            ans += abs(c1[i] - c2[i]);
        }
        l = r;
    }
    cout << ans;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…