Submission #1185903

#TimeUsernameProblemLanguageResultExecution timeMemory
1185903jerzykCoin Collecting (JOI19_ho_t4)C++20
100 / 100
26 ms2748 KiB
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1'000'000'000'000'000'000LL;
const int II = 2'000'000'000;
const ll M = 1'000'000'007LL;
const int N = 1<<18;
pair<int, int> co[N];
int s[N][2];

ll D(int a)
{
    if(a < 0) return -a;
    return a;
}

void Solve()
{
    int n;
    ll answer = 0LL;
    cin >> n;
    for(int i = 1; i <= 2 * n; ++i)
    {
        cin >> co[i].st >> co[i].nd;
        if(co[i].st < 1)
            {answer += 1 - co[i].st; co[i].st = 1;}
        if(co[i].st > n)
            {answer += co[i].st - n; co[i].st = n;}
        if(co[i].nd < 1)
            {answer += 1 - co[i].nd; co[i].nd = 1;}
        if(co[i].nd > 2)
            {answer += co[i].nd - 2; co[i].nd = 2;}
        // cout << "I: " << co[i].st << " " << co[i].nd << "\n";
        ++s[co[i].st][co[i].nd - 1];
    }
    //cout << answer << "\n";
    for(int i = 1; i <= n; ++i)
    {
        //cout << "B: " << i << " " << s[i][0] << " " << s[i][1] << "\n";
        int l = max(0, s[i][1] - 1);
        l = min(l, max(0, 1 - s[i][0]));
        s[i][0] += l; s[i][1] -= l;
        answer += D(l);

        l = max(0, s[i][0] - 1);
        l = min(l, max(0, 1 - s[i][1]));
        s[i][0] -= l; s[i][1] += l;
        answer += D(l);
        //cout << "E: " << i << " " << s[i][0] << " " << s[i][1] << "\n";

        answer += D(s[i][0] - 1);
        answer += D(s[i][1] - 1);
        s[i + 1][0] += s[i][0] - 1;
        s[i + 1][1] += s[i][1] - 1;
    }
    cout << answer << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //int t; cin >> t;
    //while(t--)
        Solve();

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