제출 #779336

#제출 시각아이디문제언어결과실행 시간메모리
779336borisAngelovCoin Collecting (JOI19_ho_t4)C++17
100 / 100
43 ms4912 KiB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 100005;

int n;

int cnt[3][maxn];

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main()
{
    fastIO();

    cin >> n;

    long long ans = 0;

    for (int i = 1; i <= 2 * n; ++i)
    {
        int x, y;
        cin >> y >> x;

        if (x <= 1)
        {
            ans += abs(1 - x);
            x = 1;
        }

        if (x >= 2)
        {
            ans += abs(x - 2);
            x = 2;
        }

        if (y <= 1)
        {
            ans += abs(1 - y);
            y = 1;
        }

        if (y >= n)
        {
            ans += abs(y - n);
            y = n;
        }

        ++cnt[x][y];
    }

    int sum1 = 0;
    int sum2 = 0;

    for (int i = 1; i <= n; ++i)
    {
        sum1 += cnt[1][i];
        sum2 += cnt[2][i];

        if (sum1 > i && sum2 < i)
        {
            int transfer = min(sum1 - i, i - sum2);
            ans += transfer;

            sum1 -= transfer;
            sum2 += transfer;
        }

        if (sum1 < i && sum2 > i)
        {
            int transfer = min(i - sum1, sum2 - i);
            ans += transfer;

            sum1 += transfer;
            sum2 -= transfer;
        }

        ans += abs(sum1 + sum2 - 2 * i);
    }

    cout << ans << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...