#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define vi vector<int>
#define ii pair<int, int>
#define vii vector<ii>
const int inf = INT_MAX;
int n, s1;
vi v, seg;
void build(int p=1, int l=0, int r=n-1) {
if (l > r) return;
if (l == r) {
seg[p] = v[l];
return;
}
int mid = (l+r)/2;
build(p*2, l, mid);
build(p*2+1, mid+1, r);
seg[p] = max(seg[p*2], seg[p*2+1]);
}
int query(int a, int b, int p=1, int l=0, int r=n-1) {
if (l > r || a > b || a > r || b < l) return -inf;
if (a <= l && r <= b) {
return seg[p];
}
int mid = (l+r)/2;
int x = query(a, b, p*2, l, mid);
int y = query(a, b, p*2+1, mid+1, r);
return max(x, y);
}
void init(int N, std::vector<int> H) {
n = N;
seg.resize(n*4);
v.resize(n);
for (int i = 0; i < n; i++) v[i] = H[i];
build();
s1 = -1;
bool b = 1;
for (int i = 1; i < n; i++) {
if (v[i] > v[i-1]) continue;
s1 = i-1;
for (int j = i+1; j < n; j++) {
if (v[j] > v[j-1]) {
b = 0;
break;
}
}
break;
}
if (!b) s1 = -1;
else if (b && s1 == -1) s1 = n-1;
}
int max_towers(int L, int R, int D) {
int l = L;
int r = R;
int d = D;
if (s1 != -1) {
if (l <= s1 && s1 <= r) {
return (max(v[l], v[r]) + d <= v[s1] ? 2 : 1);
}
return 1;
}
vii s(r-l+1);
for (int i = l; i <= r; i++) {
s[i-l] = make_pair(v[i], i);
}
sort(s.begin(), s.end());
set<int> taken;
for (int i = 0; i < r-l+1; i++) {
int h = s[i].first;
int idx = s[i].second;
auto it = taken.upper_bound(idx);
bool ok = 0;
if (it != taken.end()) {
int hi = *it;
if (h + d <= query(idx+1, hi-1)) ok = 1;
}
else ok = 1;
if (!ok) continue;
ok = 0;
if (it != taken.begin() && !taken.empty()) {
--it;
int lo = *it;
if (h + d <= query(lo+1, idx-1)) ok = 1;
}
else ok = 1;
if (ok) taken.insert(idx);
}
return taken.size();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
300 ms |
1948 KB |
Output is correct |
2 |
Correct |
928 ms |
3024 KB |
Output is correct |
3 |
Correct |
666 ms |
3004 KB |
Output is correct |
4 |
Correct |
894 ms |
2968 KB |
Output is correct |
5 |
Correct |
736 ms |
2996 KB |
Output is correct |
6 |
Correct |
736 ms |
3024 KB |
Output is correct |
7 |
Correct |
643 ms |
3024 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
16 ms |
2212 KB |
Output is correct |
37 |
Correct |
35 ms |
3060 KB |
Output is correct |
38 |
Correct |
19 ms |
3004 KB |
Output is correct |
39 |
Correct |
30 ms |
2980 KB |
Output is correct |
40 |
Correct |
17 ms |
2992 KB |
Output is correct |
41 |
Correct |
58 ms |
4160 KB |
Output is correct |
42 |
Correct |
13 ms |
3024 KB |
Output is correct |
43 |
Correct |
12 ms |
2976 KB |
Output is correct |
44 |
Correct |
19 ms |
3016 KB |
Output is correct |
45 |
Correct |
13 ms |
2976 KB |
Output is correct |
46 |
Correct |
14 ms |
3024 KB |
Output is correct |
47 |
Correct |
29 ms |
3372 KB |
Output is correct |
48 |
Correct |
26 ms |
3228 KB |
Output is correct |
49 |
Correct |
18 ms |
3096 KB |
Output is correct |
50 |
Correct |
15 ms |
2972 KB |
Output is correct |
51 |
Correct |
22 ms |
2980 KB |
Output is correct |
52 |
Correct |
67 ms |
4952 KB |
Output is correct |
53 |
Correct |
73 ms |
5720 KB |
Output is correct |
54 |
Correct |
73 ms |
5864 KB |
Output is correct |
55 |
Correct |
13 ms |
2980 KB |
Output is correct |
56 |
Correct |
55 ms |
3424 KB |
Output is correct |
57 |
Correct |
59 ms |
3876 KB |
Output is correct |
58 |
Correct |
91 ms |
4232 KB |
Output is correct |
59 |
Correct |
76 ms |
4560 KB |
Output is correct |
60 |
Correct |
68 ms |
5656 KB |
Output is correct |
61 |
Correct |
79 ms |
5300 KB |
Output is correct |
62 |
Correct |
83 ms |
4804 KB |
Output is correct |
63 |
Correct |
74 ms |
5364 KB |
Output is correct |
64 |
Correct |
13 ms |
2980 KB |
Output is correct |
65 |
Correct |
12 ms |
2980 KB |
Output is correct |
66 |
Correct |
41 ms |
3352 KB |
Output is correct |
67 |
Correct |
39 ms |
3348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4094 ms |
5052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4006 ms |
1508 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
16 ms |
2212 KB |
Output is correct |
37 |
Correct |
35 ms |
3060 KB |
Output is correct |
38 |
Correct |
19 ms |
3004 KB |
Output is correct |
39 |
Correct |
30 ms |
2980 KB |
Output is correct |
40 |
Correct |
17 ms |
2992 KB |
Output is correct |
41 |
Correct |
58 ms |
4160 KB |
Output is correct |
42 |
Correct |
13 ms |
3024 KB |
Output is correct |
43 |
Correct |
12 ms |
2976 KB |
Output is correct |
44 |
Correct |
19 ms |
3016 KB |
Output is correct |
45 |
Correct |
13 ms |
2976 KB |
Output is correct |
46 |
Correct |
14 ms |
3024 KB |
Output is correct |
47 |
Correct |
29 ms |
3372 KB |
Output is correct |
48 |
Correct |
26 ms |
3228 KB |
Output is correct |
49 |
Correct |
18 ms |
3096 KB |
Output is correct |
50 |
Correct |
15 ms |
2972 KB |
Output is correct |
51 |
Correct |
22 ms |
2980 KB |
Output is correct |
52 |
Correct |
67 ms |
4952 KB |
Output is correct |
53 |
Correct |
73 ms |
5720 KB |
Output is correct |
54 |
Correct |
73 ms |
5864 KB |
Output is correct |
55 |
Correct |
13 ms |
2980 KB |
Output is correct |
56 |
Correct |
55 ms |
3424 KB |
Output is correct |
57 |
Correct |
59 ms |
3876 KB |
Output is correct |
58 |
Correct |
91 ms |
4232 KB |
Output is correct |
59 |
Correct |
76 ms |
4560 KB |
Output is correct |
60 |
Correct |
68 ms |
5656 KB |
Output is correct |
61 |
Correct |
79 ms |
5300 KB |
Output is correct |
62 |
Correct |
83 ms |
4804 KB |
Output is correct |
63 |
Correct |
74 ms |
5364 KB |
Output is correct |
64 |
Correct |
13 ms |
2980 KB |
Output is correct |
65 |
Correct |
12 ms |
2980 KB |
Output is correct |
66 |
Correct |
41 ms |
3352 KB |
Output is correct |
67 |
Correct |
39 ms |
3348 KB |
Output is correct |
68 |
Execution timed out |
4094 ms |
5052 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
300 ms |
1948 KB |
Output is correct |
2 |
Correct |
928 ms |
3024 KB |
Output is correct |
3 |
Correct |
666 ms |
3004 KB |
Output is correct |
4 |
Correct |
894 ms |
2968 KB |
Output is correct |
5 |
Correct |
736 ms |
2996 KB |
Output is correct |
6 |
Correct |
736 ms |
3024 KB |
Output is correct |
7 |
Correct |
643 ms |
3024 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
0 ms |
208 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
0 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
208 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
16 ms |
2212 KB |
Output is correct |
47 |
Correct |
35 ms |
3060 KB |
Output is correct |
48 |
Correct |
19 ms |
3004 KB |
Output is correct |
49 |
Correct |
30 ms |
2980 KB |
Output is correct |
50 |
Correct |
17 ms |
2992 KB |
Output is correct |
51 |
Correct |
58 ms |
4160 KB |
Output is correct |
52 |
Correct |
13 ms |
3024 KB |
Output is correct |
53 |
Correct |
12 ms |
2976 KB |
Output is correct |
54 |
Correct |
19 ms |
3016 KB |
Output is correct |
55 |
Correct |
13 ms |
2976 KB |
Output is correct |
56 |
Correct |
14 ms |
3024 KB |
Output is correct |
57 |
Correct |
29 ms |
3372 KB |
Output is correct |
58 |
Correct |
26 ms |
3228 KB |
Output is correct |
59 |
Correct |
18 ms |
3096 KB |
Output is correct |
60 |
Correct |
15 ms |
2972 KB |
Output is correct |
61 |
Correct |
22 ms |
2980 KB |
Output is correct |
62 |
Correct |
67 ms |
4952 KB |
Output is correct |
63 |
Correct |
73 ms |
5720 KB |
Output is correct |
64 |
Correct |
73 ms |
5864 KB |
Output is correct |
65 |
Correct |
13 ms |
2980 KB |
Output is correct |
66 |
Correct |
55 ms |
3424 KB |
Output is correct |
67 |
Correct |
59 ms |
3876 KB |
Output is correct |
68 |
Correct |
91 ms |
4232 KB |
Output is correct |
69 |
Correct |
76 ms |
4560 KB |
Output is correct |
70 |
Correct |
68 ms |
5656 KB |
Output is correct |
71 |
Correct |
79 ms |
5300 KB |
Output is correct |
72 |
Correct |
83 ms |
4804 KB |
Output is correct |
73 |
Correct |
74 ms |
5364 KB |
Output is correct |
74 |
Correct |
13 ms |
2980 KB |
Output is correct |
75 |
Correct |
12 ms |
2980 KB |
Output is correct |
76 |
Correct |
41 ms |
3352 KB |
Output is correct |
77 |
Correct |
39 ms |
3348 KB |
Output is correct |
78 |
Execution timed out |
4094 ms |
5052 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |