Submission #1296633

#TimeUsernameProblemLanguageResultExecution timeMemory
1296633LaMatematica14The short shank; Redemption (BOI21_prison)C++20
0 / 100
178 ms26044 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    int N, D, S;
    cin >> N >> D >> S;
    vector<int> t(N);
    for (int &x : t) cin >> x;

    vector<bool> active(N, 1);
    int aus = 0;
    int saved = 0;
    for (int i= 0; i < N; i++) {
        aus = min(t[i], aus+1);
        if (t[i] > S && aus <= S) active[i] = 0;
        if (aus > S) saved++;
    }

    vector<int> P(N, -1);
    stack<pair<int, int>> st;
    st.push({-1, -1});
    int last = 0;
    for (int i = 0; i < N; i++) {
        if (!st.empty()) st.top().second = max(st.top().second, i+S-t[i]);
        if (active[i]) continue;
        while (!st.empty() && st.top().second < i) {
            P[st.top().first] = i;
            st.pop();
        } 
        st.push({i, i});
    }

    vector<vector<int>> sons(N);
    for (int i = 0; i < N; i++) {
        if (P[i] != -1) sons[P[i]].push_back(i);
    }

    vector<int> mdp(N, -1);
    vector<int> bestleaf(N, -1);
    function<void(int)> dfs = [&](int a) {
        mdp[a] = 1;
        bestleaf[a] = a;

        for (auto x : sons[a]) {
            dfs(x);
            if (mdp[a] < mdp[x]+1) {
                mdp[a] = mdp[x]+1;
                bestleaf[a] = bestleaf[x];
            }
        }
    };

    priority_queue<pair<int, int>> pq;
    for (int i = 0; i < N; i++) {
        if (P[i] == -1 && active[i] == 0) {
            dfs(i);
            pq.push({mdp[i], bestleaf[i]});
        }
    }

    
    for (; D > 0; D--) {
        saved += pq.top().first;
        int h = pq.top().second;
        pq.pop();
        while (P[h] != -1) {
            for (auto x : sons[P[h]]) {
                if (x == h) continue;
                pq.push({mdp[x], bestleaf[x]});
            }
            h = P[h];
        }
    }
    cout << N-saved << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...