Submission #1301793

#TimeUsernameProblemLanguageResultExecution timeMemory
1301793efegThe short shank; Redemption (BOI21_prison)C++20
100 / 100
343 ms229076 KiB
#include <bits/stdc++.h>
using namespace std; 

#define pb push_back
typedef pair<int,int> ii; 

using i64 = long long; 
template<typename T>
using vec = vector<T>; 

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

int n,d,t; 
vec<vec<int>> adj; 
vec<bool> pasif,rem,vis; 
vec<int> a,depth,ptr; 
vec<ii> news; 

void dfs(int node){
    if (vis[node]) return; 
    vis[node] = true; 

    int mxd = 0,idx = -1; 
    for (auto x : adj[node]){
        if (vis[x]) continue; 
        dfs(x); 
        if (depth[x] > mxd){
            idx = x;
            mxd = depth[x]; 
        }
    }

    ptr[node] = idx; 
    depth[node] = mxd + 1; 
}

void dfs2(int node){
    rem[node] = true; 
    for (auto x : adj[node]){
        if (rem[x] || x == ptr[node]) continue; 
        news.pb({depth[x],x}); 
    }

    if (ptr[node] != -1) dfs2(ptr[node]); 
}


int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); 
    cin >> n >> d >> t;
    a.assign(n+1,0);
    adj.assign(n+1,vec<int>());  
    pasif.assign(n+1,0);  
    depth.assign(n+1,0); 
    ptr.assign(n+1,0); 
    vis.assign(n+1,0); 
    rem.assign(n+1,false); 


    int minT = INT_MAX,ans = 0; 
    for (int i=1; i <= n; i++){
        cin >> a[i]; 
        chmin(minT,a[i] - i); 
        if (a[i] <= t) ans++; 
        else if(minT <= t - i){
            pasif[i] = true; 
            ans++; 
        } 
    } 

    stack<int> stk; 
    for (int i = 1; i <= n; i++){
        while (!stk.empty() && pasif[i]){
            int tp = stk.top();
            if (a[tp] - tp <= t - i) break; 
            if (pasif[tp]){
                //cerr << "Added edge: " << i << " " << tp << endl; 
                adj[i].pb(tp); 
            }
            stk.pop(); 
        }
        stk.push(i); 
    }

    priority_queue<ii> pq;
    for (int i = n; i > 0; i--){
        if (!vis[i] && pasif[i]) {
            dfs(i);
            pq.push({depth[i],i}); 
        }
    }

    int used = 0,engellendi = 0; 
    while (!pq.empty() && used < d){
        used++; 
        int node,d;tie(d,node) = pq.top();
        //cerr << node << " " << d << endl;  
        pq.pop(); 
        engellendi += d; 
        dfs2(node); 
        for (auto pi : news){
            pq.push(pi); 
        }
        news.clear(); 
    }

    cout << ans - engellendi << 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...