#include "towers.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1000000000;
vector<int> _heights;
void init(int N, std::vector<int> H) {
_heights = H;
}
struct PakTree {
int al, ar;
int rmax;
unique_ptr<PakTree> left, right;
PakTree(int i, int j, vector<int> &pts) : al(pts[i]), ar(pts[j]), rmax(-INF) {
if (i != j) {
int k = (j - i) / 2 + i;
left = make_unique<PakTree>(i, k, pts);
right = make_unique<PakTree>(k+1, j, pts);
}
}
void set(int i, int v) {
if (i < al || ar < i) return;
else if (left == nullptr) {
rmax = v;
} else {
left->set(i, v); right->set(i, v);
rmax = max(left->rmax, right->rmax);
}
}
int getMax(int i, int j) {
if (i <= al && ar <= j) return rmax;
else if (ar < i || j < al) return -INF;
else return max(left->getMax(i, j), right->getMax(i, j));
}
void _debug() {
if (left == nullptr) {
printf("(%d, %d) ", al, rmax);
} else {
left->_debug(); right->_debug();
}
}
void debug(const char *label) {
printf("%s: ", label);
_debug();
printf("\n");
}
};
int max_towers(int L, int R, int D) {
vector<int> heights(begin(_heights) + L, begin(_heights) + R + 1);
vector<int> sheights = heights;
int N = heights.size();
sort(begin(sheights), end(sheights));
PakTree lastUp(0, N - 1, sheights), lastDown(0, N - 1, sheights);
for (int v : heights) {
int ldPrev = lastDown.getMax(-INF, v - D);
int luPrev = lastUp.getMax(v + D, INF);
lastUp.set(v, ldPrev + 1);
lastDown.set(v, max(luPrev + 1, 1)); // start is always a lastDown
/*
lastUp.debug("lastUp");
lastDown.debug("lastDown");
*/
}
return lastDown.getMax(-INF, INF) / 2 + 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4011 ms |
12124 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 |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 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 |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
476 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
704 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
696 KB |
Output is correct |
31 |
Correct |
3 ms |
592 KB |
Output is correct |
32 |
Correct |
2 ms |
592 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
3 ms |
592 KB |
Output is correct |
35 |
Correct |
3 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 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 |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
476 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
704 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
696 KB |
Output is correct |
31 |
Correct |
3 ms |
592 KB |
Output is correct |
32 |
Correct |
2 ms |
592 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
3 ms |
592 KB |
Output is correct |
35 |
Correct |
3 ms |
592 KB |
Output is correct |
36 |
Correct |
42 ms |
4392 KB |
Output is correct |
37 |
Correct |
57 ms |
8108 KB |
Output is correct |
38 |
Correct |
27 ms |
3700 KB |
Output is correct |
39 |
Correct |
73 ms |
7324 KB |
Output is correct |
40 |
Correct |
26 ms |
3484 KB |
Output is correct |
41 |
Correct |
142 ms |
17324 KB |
Output is correct |
42 |
Correct |
21 ms |
1436 KB |
Output is correct |
43 |
Correct |
45 ms |
11028 KB |
Output is correct |
44 |
Correct |
17 ms |
3624 KB |
Output is correct |
45 |
Correct |
20 ms |
1556 KB |
Output is correct |
46 |
Correct |
18 ms |
1424 KB |
Output is correct |
47 |
Correct |
73 ms |
7956 KB |
Output is correct |
48 |
Correct |
45 ms |
5184 KB |
Output is correct |
49 |
Correct |
26 ms |
2588 KB |
Output is correct |
50 |
Correct |
27 ms |
5272 KB |
Output is correct |
51 |
Correct |
46 ms |
8808 KB |
Output is correct |
52 |
Correct |
213 ms |
20632 KB |
Output is correct |
53 |
Correct |
195 ms |
20760 KB |
Output is correct |
54 |
Correct |
197 ms |
20644 KB |
Output is correct |
55 |
Correct |
94 ms |
20644 KB |
Output is correct |
56 |
Correct |
91 ms |
20624 KB |
Output is correct |
57 |
Correct |
160 ms |
19940 KB |
Output is correct |
58 |
Correct |
188 ms |
20636 KB |
Output is correct |
59 |
Correct |
199 ms |
20640 KB |
Output is correct |
60 |
Correct |
237 ms |
20640 KB |
Output is correct |
61 |
Correct |
201 ms |
20640 KB |
Output is correct |
62 |
Correct |
188 ms |
20644 KB |
Output is correct |
63 |
Correct |
234 ms |
20632 KB |
Output is correct |
64 |
Correct |
68 ms |
20644 KB |
Output is correct |
65 |
Correct |
72 ms |
20640 KB |
Output is correct |
66 |
Correct |
77 ms |
20536 KB |
Output is correct |
67 |
Correct |
76 ms |
20652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4043 ms |
20120 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4026 ms |
5156 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 |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 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 |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
476 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
704 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
696 KB |
Output is correct |
31 |
Correct |
3 ms |
592 KB |
Output is correct |
32 |
Correct |
2 ms |
592 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
3 ms |
592 KB |
Output is correct |
35 |
Correct |
3 ms |
592 KB |
Output is correct |
36 |
Correct |
42 ms |
4392 KB |
Output is correct |
37 |
Correct |
57 ms |
8108 KB |
Output is correct |
38 |
Correct |
27 ms |
3700 KB |
Output is correct |
39 |
Correct |
73 ms |
7324 KB |
Output is correct |
40 |
Correct |
26 ms |
3484 KB |
Output is correct |
41 |
Correct |
142 ms |
17324 KB |
Output is correct |
42 |
Correct |
21 ms |
1436 KB |
Output is correct |
43 |
Correct |
45 ms |
11028 KB |
Output is correct |
44 |
Correct |
17 ms |
3624 KB |
Output is correct |
45 |
Correct |
20 ms |
1556 KB |
Output is correct |
46 |
Correct |
18 ms |
1424 KB |
Output is correct |
47 |
Correct |
73 ms |
7956 KB |
Output is correct |
48 |
Correct |
45 ms |
5184 KB |
Output is correct |
49 |
Correct |
26 ms |
2588 KB |
Output is correct |
50 |
Correct |
27 ms |
5272 KB |
Output is correct |
51 |
Correct |
46 ms |
8808 KB |
Output is correct |
52 |
Correct |
213 ms |
20632 KB |
Output is correct |
53 |
Correct |
195 ms |
20760 KB |
Output is correct |
54 |
Correct |
197 ms |
20644 KB |
Output is correct |
55 |
Correct |
94 ms |
20644 KB |
Output is correct |
56 |
Correct |
91 ms |
20624 KB |
Output is correct |
57 |
Correct |
160 ms |
19940 KB |
Output is correct |
58 |
Correct |
188 ms |
20636 KB |
Output is correct |
59 |
Correct |
199 ms |
20640 KB |
Output is correct |
60 |
Correct |
237 ms |
20640 KB |
Output is correct |
61 |
Correct |
201 ms |
20640 KB |
Output is correct |
62 |
Correct |
188 ms |
20644 KB |
Output is correct |
63 |
Correct |
234 ms |
20632 KB |
Output is correct |
64 |
Correct |
68 ms |
20644 KB |
Output is correct |
65 |
Correct |
72 ms |
20640 KB |
Output is correct |
66 |
Correct |
77 ms |
20536 KB |
Output is correct |
67 |
Correct |
76 ms |
20652 KB |
Output is correct |
68 |
Execution timed out |
4043 ms |
20120 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4011 ms |
12124 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |