이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
#define sz(x) int(x.size())
vi* children;
vi parent;
vi depth;
vi st_depth;
vi occ;
void dfs1(int u)
{
for(int v: children[u])
{
depth[v] = 1 + depth[u];
dfs1(v);
st_depth[u] = max(st_depth[u], 1 + st_depth[v]);
}
}
void dfs2(int u, int ct)
{
if(sz(children[u]) == 0) occ[ct]++;
else
{
bool done = 0;
for(int v: children[u])
{
if(!done && st_depth[v] == st_depth[u] - 1)
{
done = 1;
dfs2(v, ct+1);
}
else
{
dfs2(v, 1);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, D, T;
cin >> N >> D >> T;
vi t(1+N);
for(int i = 1; i <= N; i++) cin >> t[i];
vi J(1+N, -1); //place mattress in (J[i], i)
vi rst;
for(int j = N; j >= 1; j--)
{
rst.push_back(j);
while(!rst.empty())
{
int i = rst.back();
if(t[j] + (i - j) > T) break;
J[i] = j;
rst.pop_back();
}
}
// for(int i = 1; i <= N; i++) cerr << i << " : " << J[i] << '\n';
vi closings(1+N);
int res = N;
vi active(1+N, 0);
for(int i = N; i >= 1; i--)
{
if(J[i] == i)
{
continue;
}
if(J[i] == -1)
{
res--;
continue;
}
active[i] = 1;
closings[ J[i] ]++;
}
vi st{0};
children = new vi[1+N];
parent = vi(1+N);
// cerr << "res = " << res << '\n';
for(int i = N; i >= 1; i--)
{
if(active[i])
{
// cerr << "active = " << J[i] << " " << i << '\n';
parent[i] = st.back();
children[parent[i]].push_back(i);
st.push_back(i);
}
else
{
for(int z = 0; z < closings[i]; z++)
st.pop_back();
}
}
depth = vi(1+N, 0);
st_depth = vi(1+N, 0);
occ = vi(1+N, 0);
dfs1(0);
dfs2(0, 0);
for(int z = N; z >= 0; z--)
{
while(occ[z] && D)
{
occ[z]--;
D--;
res -= z;
}
}
cout << res << '\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... |