Submission #560324

#TimeUsernameProblemLanguageResultExecution timeMemory
560324messiuuuuuCoin Collecting (JOI19_ho_t4)C++14
100 / 100
73 ms4872 KiB
#include<bits/stdc++.h>
#define task "D"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 1e5 + 5;
const ll INF = 1e18 + 5;

int n;
int a[MAXN][3];
ll ans = 0;

void Input()
{
    cin >> n;
    for (int i = 1; i <= 2 * n; i++)
    {
        int x, y;
        cin >> x >> y;
        int x1 = x, y1 = y;
        if (x < 1)
            x1 = 1;
        else
            if (x > n)
                x1 = n;
        if (y < 1)
            y1 = 1;
        else
            if (y > 2)
                y1 = 2;
        ans += abs(x1 - x);
        ans += abs(y1 - y);
        a[x1][y1]++;
    }
}

void Solve()
{
    int d1 = 0, d2 = 0;
    for (int i = 1; i <= n; i++)
    {
        d1 += a[i][1] - 1;
        d2 += a[i][2] - 1;
        while (d1 > 0 && d2 < 0)
        {
            ans++;
            d1--;
            d2++;
        }
        while (d1 < 0 && d2 > 0)
        {
            ans++;
            d1++;
            d2--;
        }
        ans += abs(d1) + abs(d2);
    }
    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //freopen(task".INP","r",stdin);
    //freopen(task".OUT","w",stdout);
    Input();
    Solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...