#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(x) (int)(x).size()
typedef vector<int> vi;
void dfs(int u, vector<vi> &adj, vi &depth, vi &max_depth_node) {
max_depth_node[u] = u;
for (auto x : adj[u]) {
depth[x] = depth[u] + 1;
dfs(x, adj, depth, max_depth_node);
if (depth[max_depth_node[x]] > depth[max_depth_node[u]]) {
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);
vi passive_without_parent;
curr_min = 1e18;
for (int i = 0; i < N; i ++) {
if (is_passive[i]) {
if (curr_min <= T) {
for (auto x : passive_without_parent) {
p[x] = i;
}
passive_without_parent.clear();
curr_min = 1e18;
}
passive_without_parent.push_back(i);
}
else {
curr_min = min(curr_min + 1, t[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 depth(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) {
depth[i] = 0;
dfs(i, adj, depth, max_depth_node);
roots_at_depth[depth[max_depth_node[i]]].push_back(i);
}
}
vi visited(N);
for (int d = N - 1; d <= 0; d --) {
while (D && sz(roots_at_depth)) {
int u = max_depth_node[roots_at_depth[d].back()];
roots_at_depth[d].pop_back();
visited[u] = 1;
while (p[u] != -1) {
for (auto x : adj[u]) {
if (visited[x]) continue;
p[x] = -1;
roots_at_depth[depth[max_depth_node[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 (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... |