Submission #735741

# Submission time Handle Problem Language Result Execution time Memory
735741 2023-05-04T14:17:51 Z GrindMachine Kralj (COCI16_kralj) C++17
140 / 140
579 ms 72696 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
edi
thread: https://codeforces.com/blog/entry/47677?#comment-320681


circular array problems: try to find some cutpoint and transform into linear array problem

find a pos m s.t for any given order, nobody crosses the edge m -> m+1

let cnt(i) = #of elves that are paired up with dwarf x

how to check if given pos m is good?
starting from every pos, we will try our best to create an overflow

following should hold for all pos x:

cnt(x) + cnt(x+1) + ... + cnt(m) <= #of indices in range [x,m] (note: +1 in circular array and range in circular array)
cnt(x) + cnt(x+1) + ... + cnt(m) - (#of indices in range [x,m]) <= 0

(cnt(x)-1) + (cnt(x+1)-1) + ... + (cnt(m)-1) <= 0

replace all cnt(x) with cnt(x)-1,
cnt(x) + cnt(x+1) + ... + cnt(m) <= 0

rewrite in terms of prefix sums
let p(i) = sum[cnt(x)], x <= i (no circular stuff here)

if x > m:
p(n) - p(x) + p(x) = p(n) = 0 (p(n) = 0 because there are n dwarves and n elves)

if x <= m:
p(m) - p(x-1) <= 0

dont care abt first condition cuz p(n) is always 0
we must pick m s.t 2nd condition is satisfied

we can do this by picking m that has min val of p(m), so subtracting it with any other p(x) value would always be <= 0

once m is picked, we can cut the array at point (m,m+1):

now run greedy algo:
consider dwarfs in increasing order of new indices
when considering dwarf, first add all elves that belong to this dwarf to the active set

then, assign the elf with smallest value that has power > power of this dwarf
if such an elf doesnt exist, then assign the elf with lowest power

(idk formal proof, but sounds somewhat convincing)

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

void solve(int test_case)
{
    ll n; cin >> n;
    vector<ll> go(n + 5);
    rep1(i, n) cin >> go[i];

    vector<ll> a(n + 5), b(n + 5);
    rep1(i, n) cin >> a[i];
    rep1(i, n) cin >> b[i];

    vector<ll> cnt(n + 5);
    vector<ll> enter[n + 5];

    rep1(i, n) cnt[go[i]]++, enter[go[i]].pb(i);
    rep1(i, n) cnt[i]--;

    vector<ll> pref(n + 5);
    rep1(i, n) pref[i] = pref[i - 1] + cnt[i];

    ll m = min_element(pref.begin() + 1, pref.begin() + n + 1) - pref.begin();

    vector<ll> indices;
    for (int i = m + 1; i <= n; ++i) {
        indices.pb(i);
    }
    rep1(i, m) indices.pb(i);

    set<ll> st;
    ll ans = 0;

    trav(dwarf, indices) {
        trav(elf, enter[dwarf]) {
            st.insert(b[elf]);
        }

        auto it = st.upper_bound(a[dwarf]);
        if (it == st.end()) {
            assert(!st.empty());
            st.erase(st.begin());
        }
        else {
            ans++;
            st.erase(it);
        }
    }

    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 579 ms 59056 KB Output is correct
2 Correct 345 ms 57344 KB Output is correct
3 Correct 446 ms 71296 KB Output is correct
4 Correct 464 ms 72696 KB Output is correct
5 Correct 312 ms 50996 KB Output is correct
6 Correct 316 ms 50236 KB Output is correct
7 Correct 377 ms 54280 KB Output is correct
8 Correct 322 ms 49436 KB Output is correct
9 Correct 401 ms 57320 KB Output is correct
10 Correct 338 ms 54588 KB Output is correct