Submission #1357953

#TimeUsernameProblemLanguageResultExecution timeMemory
1357953iamhereforfunCoin Collecting (JOI19_ho_t4)C++20
100 / 100
24 ms2016 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))
#define int long long

const int N = 2e5 + 5;
const int M = 2e6 + 5;
const int O = 1e6;
const long long K = 2;
const int LG = 60;
const long long INF = 1e18 + 5;
const int C = 26;
const int B = 1000;
const int MOD = 1e9 + 7;

int n, ans, cnt[2][N], top, bot;
pair<int, int> p[N];

int dis(int a, int b, int x, int y)
{
    return abs(a - x) + abs(b - y);
}

inline void solve()
{
    cin >> n;
    ans = 0;
    for (int x = 0; x < 2 * n; x++)
    {
        int a, b;
        cin >> a >> b;
        int c, d;
        if (a > n)
        {
            c = n;
        }
        else if (a < 1)
        {
            c = 1;
        }
        else
        {
            c = a;
        }
        if (b > 2)
        {
            d = 2;
        }
        else if (b < 1)
        {
            d = 1;
        }
        else
        {
            d = b;
        }
        ans += dis(a, b, c, d);
        cnt[d - 1][c]++;
    }
    for (int x = 1; x <= n; x++)
    {
        top += cnt[0][x] - 1;
        bot += cnt[1][x] - 1;
        if (top > 0 && bot < 0)
        {
            ans += min(abs(top), abs(bot));
            if (abs(top) > abs(bot))
            {
                top += bot;
                bot = 0;
            }
            else
            {
                bot += top;
                top = 0;
            }
        }
        else if (top < 0 && bot > 0)
        {
            ans += min(abs(top), abs(bot));
            if (abs(top) > abs(bot))
            {
                top += bot;
                bot = 0;
            }
            else
            {
                bot += top;
                top = 0;
            }
        }
        ans += abs(bot) + abs(top);
    }
    cout << ans << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...