제출 #722782

#제출 시각아이디문제언어결과실행 시간메모리
722782JohannThe short shank; Redemption (BOI21_prison)C++14
100 / 100
340 ms153840 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define vb vector<bool>
#define vi vector<int>
#define vpii vector<pii>
#define vvb vector<vb>
#define vvi vector<vi>
#define vvpii vector<vpii>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

int N, D, T;
vi P;

vi protest; // state: 0 : never, 1 : save, -1 : changable
vi trigger; // the rightmost prisoner to trigger [i], only for -1

vvi adj; // graph
vi par;  // parent
vi maxSubtree;
vi nodeByLen; // number of nodes with depth [i]

void dfs(int v)
{
    if (maxSubtree[v] == -1)
    {
        if (sz(adj[v]) == 0)
        {
            maxSubtree[v] = 0;
            return;
        }
        for (int u : adj[v])
            dfs(u);
        for (int u : adj[v])
            maxSubtree[v] = max(maxSubtree[v], maxSubtree[u]);
        ++maxSubtree[v];
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> N >> D >> T;
    D = min(D, N);
    P.resize(N);
    for (int i = 0; i < N; ++i)
        cin >> P[i];

    int ans = 0;
    protest.assign(N, 0);
    trigger.assign(N, -1);
    stack<pii> blockers; // which range is blocked [i, j] inclusive
    for (int i = 0; i < N; ++i)
    {
        if (P[i] <= T)
        {
            protest[i] = 1;
            ++ans;
            blockers.push({i, min(i + T - P[i], N)});
        }
        else
        {
            while (!blockers.empty() && blockers.top().second < i)
                blockers.pop();
            if (!blockers.empty())
                protest[i] = -1, ++ans, trigger[i] = blockers.top().first;
        }
    }

    adj.resize(N);
    par.assign(N, -1);
    stack<int> todo;
    for (int i = N - 1; i >= 0; --i)
    {
        if (protest[i] != -1)
            continue;
        while (!todo.empty() && trigger[todo.top()] > i)
            todo.pop();
        if (!todo.empty())
            adj[todo.top()].push_back(i), par[i] = todo.top();
        todo.push(i);
    }

    maxSubtree.assign(N, -1);
    nodeByLen.assign(N, 0);
    for (int i = 0; i < N; ++i)
        if (protest[i] == -1)
        {
            if (maxSubtree[i] == -1)
                dfs(i);
            ++nodeByLen[maxSubtree[i]];
        }

    int stuff = 0;
    for (int i = N - 1; i >= 0; --i)
    {
        stuff = max(stuff, min(nodeByLen[i], D));
        ans -= stuff;
    }

    cout << ans << endl;

    return 0;
}
#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...