#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#define rep(i,a,b) for(int i = a; i < (b); ++i)
typedef long long ll;
#define sz(x) (int)(x).size()
typedef vector<int> vi;
typedef pair<int, int> pii;
void dfs(int u, vector<vi> &adj, vi &height, vi &max_depth_node) {
max_depth_node[u] = u;
height[u] = 0;
for (auto x : adj[u]) {
dfs(x, adj, height, max_depth_node);
if (height[x] + 1 > height[u]) {
height[u] = height[x] + 1;
max_depth_node[u] = max_depth_node[x];
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int N, D, T; cin >> N >> D >> T;
vector<ll> t(N);
for (int i = 0; i < N; i ++) cin >> t[i];
vi is_passive(N);
ll curr_min = 1e18;
for (int i = 0; i < N; i ++) {
curr_min = min(curr_min + 1, t[i]);
if (t[i] > T && curr_min <= T) is_passive[i] = 1;
}
vi p(N, -1);
stack<pair<int, ll>> passive_without_parent;
passive_without_parent.push({-1, -1}); // placeholder iniziale, non si toglie mai per le proprietà dei passive
for (int i = 0; i < N; i ++) {
// cout << "i : " << i << '\n';
// cout << "passive : " << is_passive[i] << '\n';
// if (i >= 2) return 0;
if (is_passive[i]) {
while (passive_without_parent.top().second < i) {
// se gli ultimi non passivi dopo l'ultimo passive non fa ribellare i, che è passive
// allora setto il parent dell'ultimo passive nello stack come i
auto [idx, max_rebel] = passive_without_parent.top();
passive_without_parent.pop();
p[idx] = i;
// sposto a sx il materasso e quindi unisco al passive precedente le influenze dei non passive a destra dell'ultimo passive
passive_without_parent.top().second = max(passive_without_parent.top().second, max_rebel);
}
// aggiungo l'ultimo e metto materasso alla sinistra. essendo passive, t[i] > T quindi non fa ribellare nessuno (-1)
passive_without_parent.push({i, -1});
}
else {
// aggiorno quanto in là fanno ribellare i non passive a destra dell'ultimo passive
ll max_rebel = passive_without_parent.top().second;
ll max_rebel_from_i = T + i - t[i]; // t[i] fa ribellare tutti i passive che si trovano entro T + i - t[i]
passive_without_parent.top().second = max(max_rebel, max_rebel_from_i);
}
}
vector<vi> adj(N);
for (int i = 0; i < N; i ++) {
if (!is_passive[i]) continue;
if (p[i] != -1) adj[p[i]].push_back(i);
}
vi height(N), max_depth_node(N);
vector<vi> roots_at_depth(N);
for (int i = 0; i < N; i ++) {
if (is_passive[i] && p[i] == -1) {
dfs(i, adj, height, max_depth_node);
roots_at_depth[height[i]].push_back(i);
}
}
vi visited(N);
for (int d = N - 1; d >= 0; d --) {
while (D && !roots_at_depth[d].empty()) {
int r = roots_at_depth[d].back();
roots_at_depth[d].pop_back();
if (visited[r]) continue;
int u = max_depth_node[r]; ;
while (u != -1) {
for (auto x : adj[u]) {
if (visited[x]) continue;
p[x] = -1;
roots_at_depth[height[x]].push_back(x);
}
visited[u] = 1;
u = p[u];
}
D --;
}
}
ll sol = 0;
for (int i = 0; i < N; i ++) {
if (!is_passive[i] && t[i] <= T) sol ++;
if (is_passive[i] && !visited[i]) sol ++;
}
cout << sol << '\n';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |