#include "towers.h"
#include <vector>
#include <iostream>
typedef std::vector<int> vi;
typedef std::vector<vi> vvi;
std::vector<int> real_h(0);
vvi maxi(0);
int total_maxi = 0;
void init_maxi(int N) {
maxi.resize(N, vi(0));
for (int i = 0; i < N; i++) {
maxi[i].push_back(i);
}
int size = 2;
while (size <= N) {
for (int i = 0; i < N; i += size) {
int value = maxi[i][maxi[i].size() - 1];
int next_place = i + (size / 2);
if (next_place < N) {
int other_value = maxi[next_place][maxi[next_place].size() - 1];
if (real_h[other_value] > real_h[value]) {
value = other_value;
}
}
maxi[i].push_back(value);
}
size *= 2;
}
}
void init(int N, std::vector<int> H) {
real_h.resize(N);
for (int i = 0; i < N; i++) {
real_h[i] = H[i];
if (real_h[i] > total_maxi) {
total_maxi = real_h[i];
}
}
init_maxi(N);
}
int get_max_id2(int l, int r) {
if (l == r) {
return l;
}
int size = 1;
for (int i = 1; i < maxi[l].size(); i++) {
if (l + size + size - 1 > r) {
int temp_maxi = maxi[l][i - 1];
if (l + size - 1 == r) {
return temp_maxi;
}
else {
int other_res = get_max_id2(l + size, r);
if (real_h[other_res] > real_h[temp_maxi]) {
return other_res;
}
return temp_maxi;
}
}
size *= 2;
}
int temp_maxi = maxi[l][maxi[l].size() - 1];
if (l + size - 1 == r) {
return temp_maxi;
}
else {
int other_res = get_max_id2(l + size, r);
if (real_h[other_res] > real_h[temp_maxi]) {
return other_res;
}
return temp_maxi;
}
}
int get_max_id(int l, int r) {
int max_result = real_h[l];
int max_id =l;
for (int i = l + 1; i <= r; i++) {
if (real_h[i] > max_result) {
max_result = real_h[i];
max_id = i;
}
}
return max_id;
}
int get_min_id(int l, int r) {
int min_result= real_h[l];
int min_id =l;
for (int i = l + 1; i <= r; i++) {
if (real_h[i] < min_result) {
min_result = real_h[i];
min_id = i;
}
}
return min_id;
}
int max_towers2(int l, int r, int d, int max_possible_value) {
if (l > r) {
return 0;
}
if (l == r) {
if (real_h[l] <= max_possible_value) {
return 1;
}
return 0;
}
if (l + 1 == r) {
if (real_h[l] <= max_possible_value || real_h[l + 1] <= max_possible_value) {
return 1;
}
return 0;
}
int max_id = get_max_id2(l, r);
int left_value = max_towers2(l, max_id - 1, d, real_h[max_id] - d);
int right_value = max_towers2(max_id + 1, r, d, real_h[max_id] - d);
if (left_value + right_value == 0) {
int min_id = get_min_id(l, r);
if (real_h[min_id] <= max_possible_value) {
return 1;
}
}
return left_value + right_value;
}
int max_towers(int L, int R, int D) {
return max_towers2(L, R, D, total_maxi);
}
/*int main() {
init(7, std::vector<int>{10, 20, 60, 40, 50, 30, 70});
std::cout << max_towers(1, 5, 10) << std::endl;
std::cout << max_towers(2, 2, 100) << std::endl;
std::cout << max_towers(0, 6, 17) << std::endl;
}//*/
Compilation message
towers.cpp: In function 'int get_max_id2(int, int)':
towers.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 1; i < maxi[l].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4022 ms |
7848 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
464 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 |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 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 |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 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 |
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 |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
464 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 |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 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 |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 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 |
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 |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
17 ms |
4768 KB |
Output is correct |
37 |
Correct |
30 ms |
7200 KB |
Output is correct |
38 |
Correct |
21 ms |
7240 KB |
Output is correct |
39 |
Correct |
24 ms |
7172 KB |
Output is correct |
40 |
Correct |
22 ms |
7204 KB |
Output is correct |
41 |
Correct |
28 ms |
7204 KB |
Output is correct |
42 |
Correct |
22 ms |
7220 KB |
Output is correct |
43 |
Correct |
151 ms |
10768 KB |
Output is correct |
44 |
Correct |
79 ms |
7928 KB |
Output is correct |
45 |
Correct |
25 ms |
7180 KB |
Output is correct |
46 |
Correct |
22 ms |
7244 KB |
Output is correct |
47 |
Correct |
24 ms |
7204 KB |
Output is correct |
48 |
Correct |
21 ms |
7248 KB |
Output is correct |
49 |
Correct |
22 ms |
7164 KB |
Output is correct |
50 |
Correct |
29 ms |
8564 KB |
Output is correct |
51 |
Correct |
29 ms |
9912 KB |
Output is correct |
52 |
Correct |
25 ms |
7216 KB |
Output is correct |
53 |
Correct |
23 ms |
7196 KB |
Output is correct |
54 |
Correct |
29 ms |
7248 KB |
Output is correct |
55 |
Correct |
49 ms |
14688 KB |
Output is correct |
56 |
Correct |
38 ms |
13888 KB |
Output is correct |
57 |
Correct |
24 ms |
6936 KB |
Output is correct |
58 |
Correct |
24 ms |
7196 KB |
Output is correct |
59 |
Correct |
27 ms |
7240 KB |
Output is correct |
60 |
Correct |
30 ms |
7196 KB |
Output is correct |
61 |
Correct |
24 ms |
7204 KB |
Output is correct |
62 |
Correct |
23 ms |
7244 KB |
Output is correct |
63 |
Correct |
24 ms |
7196 KB |
Output is correct |
64 |
Correct |
909 ms |
14692 KB |
Output is correct |
65 |
Correct |
734 ms |
14696 KB |
Output is correct |
66 |
Correct |
466 ms |
11640 KB |
Output is correct |
67 |
Correct |
564 ms |
14232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4011 ms |
7116 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4086 ms |
1956 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
464 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 |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 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 |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 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 |
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 |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
17 ms |
4768 KB |
Output is correct |
37 |
Correct |
30 ms |
7200 KB |
Output is correct |
38 |
Correct |
21 ms |
7240 KB |
Output is correct |
39 |
Correct |
24 ms |
7172 KB |
Output is correct |
40 |
Correct |
22 ms |
7204 KB |
Output is correct |
41 |
Correct |
28 ms |
7204 KB |
Output is correct |
42 |
Correct |
22 ms |
7220 KB |
Output is correct |
43 |
Correct |
151 ms |
10768 KB |
Output is correct |
44 |
Correct |
79 ms |
7928 KB |
Output is correct |
45 |
Correct |
25 ms |
7180 KB |
Output is correct |
46 |
Correct |
22 ms |
7244 KB |
Output is correct |
47 |
Correct |
24 ms |
7204 KB |
Output is correct |
48 |
Correct |
21 ms |
7248 KB |
Output is correct |
49 |
Correct |
22 ms |
7164 KB |
Output is correct |
50 |
Correct |
29 ms |
8564 KB |
Output is correct |
51 |
Correct |
29 ms |
9912 KB |
Output is correct |
52 |
Correct |
25 ms |
7216 KB |
Output is correct |
53 |
Correct |
23 ms |
7196 KB |
Output is correct |
54 |
Correct |
29 ms |
7248 KB |
Output is correct |
55 |
Correct |
49 ms |
14688 KB |
Output is correct |
56 |
Correct |
38 ms |
13888 KB |
Output is correct |
57 |
Correct |
24 ms |
6936 KB |
Output is correct |
58 |
Correct |
24 ms |
7196 KB |
Output is correct |
59 |
Correct |
27 ms |
7240 KB |
Output is correct |
60 |
Correct |
30 ms |
7196 KB |
Output is correct |
61 |
Correct |
24 ms |
7204 KB |
Output is correct |
62 |
Correct |
23 ms |
7244 KB |
Output is correct |
63 |
Correct |
24 ms |
7196 KB |
Output is correct |
64 |
Correct |
909 ms |
14692 KB |
Output is correct |
65 |
Correct |
734 ms |
14696 KB |
Output is correct |
66 |
Correct |
466 ms |
11640 KB |
Output is correct |
67 |
Correct |
564 ms |
14232 KB |
Output is correct |
68 |
Execution timed out |
4011 ms |
7116 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4022 ms |
7848 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |