제출 #1273007

#제출 시각아이디문제언어결과실행 시간메모리
1273007MunkhErdeneCoin Collecting (JOI19_ho_t4)C++17
100 / 100
55 ms40612 KiB
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define lll __int128
#define ll long long
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll naim = 1e9;
const ll max_bit = 60;
const ull tom = ULLONG_MAX;
const ll MAXN = 100005;
const ll LOG = 20;
const ll NAIM = 1e18;
const ll N = 2e6 + 5;
int main() {
    #define pb push_back
    #define ff first
    #define ss second
    #define _ << " " <<
    #define yes cout<<"YES\n"
    #define no cout<<"NO\n"
    #define all(x) x.begin(),x.end()
    #define rall(x) x.rbegin(),x.rend()
    #define BlueCrowner ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define FOR(i, a, b) for (ll i = (a); i < (b); i++)
    #define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
    // ---------- GCD ----------
    auto gcd = [&](ll a, ll b) {
        while (b) {
            a %= b;
            swap(a, b);
        }
        return a;
    };
    // ---------- LCM ----------
    auto lcm = [&](ll a, ll b) {
        return a / gcd(a, b) * b;
    };
    // ---------- Modular Exponentiation ----------
    function<ll(ll, ll, ll)> modpow = [&](ll a, ll b, ll m) {
        ll c = 1;
        a %= m;
        while (b > 0) {
            if (b & 1) c = c * a % m;
            a = a * a % m;
            b >>= 1;
        }
        return c;
    };
    // ---------- Modular Inverse (Fermat’s Little Theorem) ----------
    function<ll(ll, ll)> modinv = [&](ll a, ll m) {
        return modpow(a, m - 2, m);
    };
    // ---------- Factorials and Inverse Factorials ----------
    vector<ll> fact(N), invfact(N);
    //a ^ -1
    auto pre_fact = [&](ll n = N-1, ll m = mod) {
        fact[0] = 1;
        for (ll i = 1; i <= n; i++) fact[i] = fact[i-1] * i % m;
        invfact[n] = modinv(fact[n], m);
        for (ll i = n; i > 0; i--) invfact[i-1] = invfact[i] * i % m;
    };
    // ---------- nCr ----------
    auto nCr = [&](ll n, ll r, ll m = mod) {
        if (r < 0 || r > n) return 0LL;
        return fact[n] * invfact[r] % m * invfact[n-r] % m;
    };
    // ---------- Sieve of Eratosthenes ----------
    vector<ll> primes;
    vector<bool> is_prime(N);
    auto sieve = [&](ll n = N-1) {
        fill(is_prime.begin(), is_prime.begin() + n + 1, true);
        is_prime[0] = is_prime[1] = false;
        for (ll i = 2; i * i <= n; i++) {
            if (is_prime[i]) {
                for (ll j = i * i; j <= n; j += i)
                    is_prime[j] = false;
            }
        }
        for (ll i = 2; i <= n; i++)
            if (is_prime[i]) primes.pb(i);
    };
    function<void()> solve = [&]() {
        ll n; cin >> n;
        vector<pair<ll, ll>> a(n * 2); for(auto &x : a) cin >> x.ff >> x.ss;
        vector<vector<ll>> cnt(n + 1, vector<ll> (3, 0ll));
        ll ans = 0;
        FOR(i, 0, 2 * n){
            if(a[i].ff < 1 && a[i].ss <= 1) cnt[1][1]++, ans += abs(1 - a[i].ff) + abs(1 - a[i].ss);
            else if(a[i].ff < 1 && a[i].ss >= 2) cnt[1][2]++, ans += abs(1 - a[i].ff) + abs(2 - a[i].ss); 
            else if(a[i].ff > n && a[i].ss <= 1) cnt[n][1]++, ans += abs(n - a[i].ff) + abs(1 - a[i].ss);
            else if(a[i].ff > n && a[i].ss >= 2) cnt[n][2]++, ans += abs(n - a[i].ff) + abs(2 - a[i].ss);
            else if(a[i].ss <= 1) cnt[a[i].ff][1]++, ans += abs(1 - a[i].ss);
            else cnt[a[i].ff][2]++, ans += abs(2 - a[i].ss);
        }
        ll d1 = 0, d2 = 0;
        FOR(x, 1, n + 1){
            d1 += cnt[x][1] - 1;
            d2 += cnt[x][2] - 1;
            if(d1 > 0 && d2 < 0){
                ll t = min(d1, -d2);
                ans += t;
                d1 -= t;
                d2 += t;
            }
            else if(d1 < 0 && d2 > 0){
                ll t = min(-d1, d2);
                ans += t;
                d1 += t;
                d2 -= t;
            }
            ans += abs(d1) + abs(d2);
        }
        cout << ans << '\n';
    };
    BlueCrowner;
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...