Submission #447406

#TimeUsernameProblemLanguageResultExecution timeMemory
447406prvocisloThe short shank; Redemption (BOI21_prison)C++17
100 / 100
425 ms133796 KiB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, D, T;
    cin >> N >> D >> T;
    vector<pair<int, int> > stk;
    vector<int> t(N), d(N), ch(N, -1);
    vector<vector<int> > g(N);
    int maxi = -1, ans = N;
    for (int i = 0; i < N; i++)
    {
        cin >> t[i];
        int ti = T + i - t[i]; 
        maxi = max(maxi, ti);
        if (!stk.empty()) stk.back().second = max(stk.back().second, ti);
        if (t[i] > T)
        {
            if (i > maxi) ans--;
            else // novy pasivny zlocinec
            {
                d[i] = 1;
                while (!stk.empty() && i > stk.back().second)
                {
                    int j = stk.back().first, val = stk.back().second;
                    stk.pop_back();
                    if (!stk.empty()) stk.back().second = max(stk.back().second, val);
                    if (d[i] < d[j]+1)
                    {
                        ch[i] = j;
                        d[i] = d[j]+1;
                    }
                    g[i].push_back(j);
                }
                stk.push_back({i, ti});
            }
        }
    }
    priority_queue<pair<int, int> > pq;
    while (!stk.empty())
    {
        pq.push({d[stk.back().first], stk.back().first});
        stk.pop_back();
    }
    while (D-- && !pq.empty())
    {
        int vr = pq.top().second;
        pq.pop();
        ans -= d[vr];
        while (vr != -1)
        {
            for (int i : g[vr]) if (i != ch[vr]) pq.push({d[i], i});
            vr = ch[vr];
        }
    }
    cout << ans << "\n";
    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...