#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
int N, p;
vector<int> H;
void init(int n, vector<int> h) {
N = n, H = h, p = -1;
for (int i = 0; i < N; i++) {
if ((i == 0 || H[i] > H[i - 1]) && (i == N - 1 || H[i] > H[i + 1])) {
p = i;
}
}
for (int i = 0; i < p; i++) {
if (H[i] > H[i + 1]) {
p = -1;
return;
}
}
for (int i = p + 1; i < N; i++) {
if (H[i - 1] < H[i]) {
p = -1;
return;
}
}
}
int max_towers(int L, int R, int D) {
if (p != -1) {
int l = upper_bound(H.begin(), H.begin() + p, H[p] - D) - H.begin() - 1;
int r = lower_bound(H.begin() + p, H.end(), H[p] - D, [&](int x, int y) {return x > y;}) - H.begin();
return (L <= l && r <= R) ? 2 : 1;
}
vector<int> lg(N + 1);
for (int i = 2; i <= N; i++) {
lg[i] = lg[i / 2] + 1;
}
vector st(N, vector<int>(lg[N] + 1));
for (int i = 0; i < N; i++) {
st[i][0] = i;
}
auto f = [&](int i, int j) {
if (i == -1) {
return j;
}
if (j == -1) {
return i;
}
return H[i] > H[j] ? i : j;
};
for (int j = 0; j < lg[N]; j++) {
for (int i = 0; i + (2 << j) <= N; i++) {
st[i][j + 1] = f(st[i][j], st[i + (1 << j)][j]);
}
}
auto query = [&](int l, int r) {
if (l == r) {
return -1;
}
int k = lg[r - l];
return f(st[l][k], st[r - (1 << k)][k]);
};
if (D == 1) {
auto rec = [&](auto rec, int l, int r) -> int {
if (l + 1 >= r) {
return r - l;
}
int m = query(l, r);
return rec(rec, l, m) + rec(rec, m + 1, r);
};
return rec(rec, L, R + 1);
}
vector<int> l(N, -1), r(N, N);
for (int i = 0; i < N; i++) {
if (H[query(0, i)] >= H[i] + D) {
int lo = 0, hi = i - 1;
while (lo < hi) {
int mi = (lo + hi + 1) / 2;
if (H[query(mi, i)] >= H[i] + D) {
lo = mi;
} else {
hi = mi - 1;
}
}
l[i] = lo;
}
if (H[query(i + 1, N)] >= H[i] + D) {
int lo = i + 1, hi = N - 1;
while (lo < hi) {
int mi = (lo + hi) / 2;
if (H[query(i + 1, mi + 1)] >= H[i] + D) {
hi = mi;
} else {
lo = mi + 1;
}
}
r[i] = lo;
}
}
vector<int> dp(N + 1), t(N + 1);
for (int i = L; i <= R; i++) {
t[i + 1] = max(t[i + 1], t[i]);
dp[i + 1] = max(dp[i], t[l[i] + 1] + 1);
if (r[i] < N) {
t[r[i] + 1] = max(t[r[i] + 1], dp[i + 1]);
}
}
return dp[R + 1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
928 KB |
Output is correct |
2 |
Correct |
660 ms |
1440 KB |
Output is correct |
3 |
Correct |
678 ms |
1440 KB |
Output is correct |
4 |
Correct |
777 ms |
1360 KB |
Output is correct |
5 |
Correct |
624 ms |
1360 KB |
Output is correct |
6 |
Correct |
695 ms |
1460 KB |
Output is correct |
7 |
Correct |
699 ms |
1360 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
208 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
208 KB |
Output is correct |
33 |
Correct |
1 ms |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
208 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
208 KB |
Output is correct |
33 |
Correct |
1 ms |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
30 ms |
8648 KB |
Output is correct |
37 |
Correct |
33 ms |
13184 KB |
Output is correct |
38 |
Correct |
46 ms |
13188 KB |
Output is correct |
39 |
Correct |
26 ms |
13076 KB |
Output is correct |
40 |
Correct |
51 ms |
13188 KB |
Output is correct |
41 |
Correct |
48 ms |
13188 KB |
Output is correct |
42 |
Correct |
58 ms |
13096 KB |
Output is correct |
43 |
Correct |
11 ms |
1444 KB |
Output is correct |
44 |
Correct |
9 ms |
1488 KB |
Output is correct |
45 |
Correct |
24 ms |
13120 KB |
Output is correct |
46 |
Correct |
35 ms |
13192 KB |
Output is correct |
47 |
Correct |
26 ms |
11552 KB |
Output is correct |
48 |
Correct |
20 ms |
11560 KB |
Output is correct |
49 |
Correct |
20 ms |
11588 KB |
Output is correct |
50 |
Correct |
14 ms |
1448 KB |
Output is correct |
51 |
Correct |
21 ms |
13064 KB |
Output is correct |
52 |
Correct |
26 ms |
11564 KB |
Output is correct |
53 |
Correct |
28 ms |
11560 KB |
Output is correct |
54 |
Correct |
20 ms |
11656 KB |
Output is correct |
55 |
Correct |
9 ms |
1440 KB |
Output is correct |
56 |
Correct |
23 ms |
15016 KB |
Output is correct |
57 |
Correct |
35 ms |
12700 KB |
Output is correct |
58 |
Correct |
42 ms |
13196 KB |
Output is correct |
59 |
Correct |
60 ms |
13184 KB |
Output is correct |
60 |
Correct |
52 ms |
13120 KB |
Output is correct |
61 |
Correct |
49 ms |
13188 KB |
Output is correct |
62 |
Correct |
43 ms |
13172 KB |
Output is correct |
63 |
Correct |
47 ms |
13096 KB |
Output is correct |
64 |
Correct |
9 ms |
1360 KB |
Output is correct |
65 |
Correct |
9 ms |
1448 KB |
Output is correct |
66 |
Correct |
39 ms |
13096 KB |
Output is correct |
67 |
Correct |
24 ms |
13188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4103 ms |
11872 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4027 ms |
3528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
208 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
208 KB |
Output is correct |
33 |
Correct |
1 ms |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
30 ms |
8648 KB |
Output is correct |
37 |
Correct |
33 ms |
13184 KB |
Output is correct |
38 |
Correct |
46 ms |
13188 KB |
Output is correct |
39 |
Correct |
26 ms |
13076 KB |
Output is correct |
40 |
Correct |
51 ms |
13188 KB |
Output is correct |
41 |
Correct |
48 ms |
13188 KB |
Output is correct |
42 |
Correct |
58 ms |
13096 KB |
Output is correct |
43 |
Correct |
11 ms |
1444 KB |
Output is correct |
44 |
Correct |
9 ms |
1488 KB |
Output is correct |
45 |
Correct |
24 ms |
13120 KB |
Output is correct |
46 |
Correct |
35 ms |
13192 KB |
Output is correct |
47 |
Correct |
26 ms |
11552 KB |
Output is correct |
48 |
Correct |
20 ms |
11560 KB |
Output is correct |
49 |
Correct |
20 ms |
11588 KB |
Output is correct |
50 |
Correct |
14 ms |
1448 KB |
Output is correct |
51 |
Correct |
21 ms |
13064 KB |
Output is correct |
52 |
Correct |
26 ms |
11564 KB |
Output is correct |
53 |
Correct |
28 ms |
11560 KB |
Output is correct |
54 |
Correct |
20 ms |
11656 KB |
Output is correct |
55 |
Correct |
9 ms |
1440 KB |
Output is correct |
56 |
Correct |
23 ms |
15016 KB |
Output is correct |
57 |
Correct |
35 ms |
12700 KB |
Output is correct |
58 |
Correct |
42 ms |
13196 KB |
Output is correct |
59 |
Correct |
60 ms |
13184 KB |
Output is correct |
60 |
Correct |
52 ms |
13120 KB |
Output is correct |
61 |
Correct |
49 ms |
13188 KB |
Output is correct |
62 |
Correct |
43 ms |
13172 KB |
Output is correct |
63 |
Correct |
47 ms |
13096 KB |
Output is correct |
64 |
Correct |
9 ms |
1360 KB |
Output is correct |
65 |
Correct |
9 ms |
1448 KB |
Output is correct |
66 |
Correct |
39 ms |
13096 KB |
Output is correct |
67 |
Correct |
24 ms |
13188 KB |
Output is correct |
68 |
Execution timed out |
4103 ms |
11872 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
928 KB |
Output is correct |
2 |
Correct |
660 ms |
1440 KB |
Output is correct |
3 |
Correct |
678 ms |
1440 KB |
Output is correct |
4 |
Correct |
777 ms |
1360 KB |
Output is correct |
5 |
Correct |
624 ms |
1360 KB |
Output is correct |
6 |
Correct |
695 ms |
1460 KB |
Output is correct |
7 |
Correct |
699 ms |
1360 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
0 ms |
208 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
208 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
0 ms |
208 KB |
Output is correct |
23 |
Correct |
0 ms |
208 KB |
Output is correct |
24 |
Correct |
1 ms |
208 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
208 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
464 KB |
Output is correct |
42 |
Correct |
1 ms |
208 KB |
Output is correct |
43 |
Correct |
1 ms |
208 KB |
Output is correct |
44 |
Correct |
1 ms |
464 KB |
Output is correct |
45 |
Correct |
1 ms |
464 KB |
Output is correct |
46 |
Correct |
30 ms |
8648 KB |
Output is correct |
47 |
Correct |
33 ms |
13184 KB |
Output is correct |
48 |
Correct |
46 ms |
13188 KB |
Output is correct |
49 |
Correct |
26 ms |
13076 KB |
Output is correct |
50 |
Correct |
51 ms |
13188 KB |
Output is correct |
51 |
Correct |
48 ms |
13188 KB |
Output is correct |
52 |
Correct |
58 ms |
13096 KB |
Output is correct |
53 |
Correct |
11 ms |
1444 KB |
Output is correct |
54 |
Correct |
9 ms |
1488 KB |
Output is correct |
55 |
Correct |
24 ms |
13120 KB |
Output is correct |
56 |
Correct |
35 ms |
13192 KB |
Output is correct |
57 |
Correct |
26 ms |
11552 KB |
Output is correct |
58 |
Correct |
20 ms |
11560 KB |
Output is correct |
59 |
Correct |
20 ms |
11588 KB |
Output is correct |
60 |
Correct |
14 ms |
1448 KB |
Output is correct |
61 |
Correct |
21 ms |
13064 KB |
Output is correct |
62 |
Correct |
26 ms |
11564 KB |
Output is correct |
63 |
Correct |
28 ms |
11560 KB |
Output is correct |
64 |
Correct |
20 ms |
11656 KB |
Output is correct |
65 |
Correct |
9 ms |
1440 KB |
Output is correct |
66 |
Correct |
23 ms |
15016 KB |
Output is correct |
67 |
Correct |
35 ms |
12700 KB |
Output is correct |
68 |
Correct |
42 ms |
13196 KB |
Output is correct |
69 |
Correct |
60 ms |
13184 KB |
Output is correct |
70 |
Correct |
52 ms |
13120 KB |
Output is correct |
71 |
Correct |
49 ms |
13188 KB |
Output is correct |
72 |
Correct |
43 ms |
13172 KB |
Output is correct |
73 |
Correct |
47 ms |
13096 KB |
Output is correct |
74 |
Correct |
9 ms |
1360 KB |
Output is correct |
75 |
Correct |
9 ms |
1448 KB |
Output is correct |
76 |
Correct |
39 ms |
13096 KB |
Output is correct |
77 |
Correct |
24 ms |
13188 KB |
Output is correct |
78 |
Execution timed out |
4103 ms |
11872 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |