#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = numeric_limits<int> :: max();
struct ST{
vector<int> t, p;
int n;
ST(int ns){
n = ns;
t.resize(4 * n);
p.resize(4 * n);
}
void push(int& v, int& vv){
if (!p[v]) return;
p[vv] += p[v]; t[vv] += p[v];
p[vv + 1] += p[v]; t[vv + 1] += p[v];
p[v] = 0;
}
void add(int v, int tl, int tr, int& l, int& r, int& x){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
t[v] += x;
p[v] += x;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v, vv);
add(vv, tl, tm, l, r, x);
add(vv + 1, tm + 1, tr, l, r, x);
t[v] = min(t[vv], t[vv + 1]);
}
void add(int l, int r, int x){
if (l > r) return;
add(1, 1, n, l, r, x);
}
int get(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return inf;
if (l <= tl && tr <= r){
return t[v];
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v, vv);
return min(get(vv, tl, tm, l, r), get(vv + 1, tm + 1, tr, l, r));
}
int get(int l, int r){
if (l > r) return inf;
return get(1, 1, n, l, r);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, d, t; cin>>n>>d>>t;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++){
cin>>a[i];
}
vector<int> h(n + 1);
priority_queue<int, vector<int>, greater<int>> pq;
int tt = 0, cnt = 0;
for (int j = 1; j <= n; j++){
pq.push(a[j] - tt);
int m = pq.top() + tt;
cnt += (m <= t);
h[j] = cnt;
tt++;
}
vector<int> r(n + 1);
for (int i = 1; i <= n; i++){
if (a[i] > t) continue;
int m = t - a[i];
r[i] = i + m;
}
const int lg = ceil(log2(n));
vector<vector<int>> sp(n + 1, vector<int>(lg));
for (int i = 1; i <= n; i++){
sp[i][0] = r[i];
}
for (int j = 1; j < lg; j++){
for (int i = 1; i + (1 << j) <= n + 1; i++){
sp[i][j] = max(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
}
}
vector<int> log(n + 1);
for (int i = 2; i <= n; i++){
log[i] = log[i / 2] + 1;
}
auto check = [&](int x, int y){
int k = log[y - x + 1];
return max(sp[x][k], sp[y - (1 << k) + 1][k]) >= y;
};
vector<int> f(n);
for (int i = 1; i < n; i++){
int j = 2;
while (j <= i + 1 && check(j, i + 1)){
j++;
}
f[i] = j - 1;
}
vector<vector<int>> dp(n + 1, vector<int>(d + 1));
for (int i = 1; i <= n; i++){
dp[i][0] = h[i];
}
for (int k = 1; k <= d; k++){
ST T(n);
for (int i = 1; i <= n; i++){
T.add(1, f[i - 1] - 1, 1);
dp[i][k] = min(dp[i][k - 1], T.get(1, i - 1));
T.add(i, i, dp[i][k - 1]);
}
}
cout<<dp[n][d]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
644 KB |
Output is correct |
5 |
Correct |
12 ms |
732 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
7 ms |
616 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
640 KB |
Output is correct |
11 |
Correct |
13 ms |
740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
2066 ms |
75720 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
644 KB |
Output is correct |
5 |
Correct |
12 ms |
732 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
7 ms |
616 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
640 KB |
Output is correct |
11 |
Correct |
13 ms |
740 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
604 KB |
Output is correct |
16 |
Correct |
13 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
568 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
640 KB |
Output is correct |
22 |
Correct |
11 ms |
604 KB |
Output is correct |
23 |
Correct |
146 ms |
2716 KB |
Output is correct |
24 |
Correct |
276 ms |
4292 KB |
Output is correct |
25 |
Correct |
401 ms |
5852 KB |
Output is correct |
26 |
Correct |
525 ms |
7260 KB |
Output is correct |
27 |
Correct |
638 ms |
8988 KB |
Output is correct |
28 |
Correct |
91 ms |
2092 KB |
Output is correct |
29 |
Correct |
100 ms |
2240 KB |
Output is correct |
30 |
Correct |
171 ms |
3032 KB |
Output is correct |
31 |
Correct |
305 ms |
5108 KB |
Output is correct |
32 |
Correct |
154 ms |
2952 KB |
Output is correct |
33 |
Correct |
175 ms |
3188 KB |
Output is correct |
34 |
Correct |
75 ms |
1884 KB |
Output is correct |
35 |
Correct |
635 ms |
8984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
2048 ms |
10524 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
644 KB |
Output is correct |
5 |
Correct |
12 ms |
732 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
7 ms |
616 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
640 KB |
Output is correct |
11 |
Correct |
13 ms |
740 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
604 KB |
Output is correct |
16 |
Correct |
13 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
568 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
640 KB |
Output is correct |
22 |
Correct |
11 ms |
604 KB |
Output is correct |
23 |
Correct |
146 ms |
2716 KB |
Output is correct |
24 |
Correct |
276 ms |
4292 KB |
Output is correct |
25 |
Correct |
401 ms |
5852 KB |
Output is correct |
26 |
Correct |
525 ms |
7260 KB |
Output is correct |
27 |
Correct |
638 ms |
8988 KB |
Output is correct |
28 |
Correct |
91 ms |
2092 KB |
Output is correct |
29 |
Correct |
100 ms |
2240 KB |
Output is correct |
30 |
Correct |
171 ms |
3032 KB |
Output is correct |
31 |
Correct |
305 ms |
5108 KB |
Output is correct |
32 |
Correct |
154 ms |
2952 KB |
Output is correct |
33 |
Correct |
175 ms |
3188 KB |
Output is correct |
34 |
Correct |
75 ms |
1884 KB |
Output is correct |
35 |
Correct |
635 ms |
8984 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Execution timed out |
2048 ms |
10524 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
644 KB |
Output is correct |
5 |
Correct |
12 ms |
732 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
7 ms |
616 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
640 KB |
Output is correct |
11 |
Correct |
13 ms |
740 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2066 ms |
75720 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |