Submission #1089500

#TimeUsernameProblemLanguageResultExecution timeMemory
1089500ivan_alexeevCoin Collecting (JOI19_ho_t4)C++17
100 / 100
35 ms4948 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

#ifdef lisie_bimbi
#else
#define endl '\n'
#endif
typedef long long ll;

const int inf = 1000000000;

using namespace std;

#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,bmi2,fma,popcnt")


//#define int long long

int c[100000][2]{};

void solve(){
    int n;
    cin >> n;
    ll ans = 0;
    for(int i = 0; i < 2 * n; i++){
        int x, y;
        cin >> x >> y;
        int x1 = x, y1 = y;
        if(x <= 0){
            x = 1;
        }else if(x > n){
            x = n;
        }
        if(y <= 0){
            y = 1;
        }else if(y > 2){
            y = 2;
        }
        ans += abs(x1 - x) + abs(y1 - y);
        c[x - 1][y - 1]++;
    }
    int now1 = 0, now2 = 0;
    for(int i = 0; i < n; i++){
        now1 += c[i][0] - 1;
        now2 += c[i][1] - 1;
        if((now1 > 0) && (now2 < 0)){
            int k = min(now1, -now2);
            now1 -= k;
            now2 += k;
            ans += k;
        }else if((now1 < 0) && (now2 > 0)){
            int k = min(-now1, now2);
            now1 += k;
            now2 -= k;
            ans += k;
        }
        ans += abs(now1) + abs(now2);
    }
    cout << ans << endl;
}

signed main(){
#ifdef lisie_bimbi
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    cin.tie(nullptr)->sync_with_stdio(false);
#endif
    cout << setprecision(5) << fixed;
    int _ = 1;
    //cin >> t;
    while(_--){
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...