#include "towers.h"
#include <vector>
#include <set>
#include <iostream>
#include <queue>
using namespace std;
// The key observation.
// - All towers have a 'banned region' that spans some elements
// on both sides of the tower. It ends to either to the end
// of the array, or to the next element with height larger
// by at least D.
//
// Some further observations on 'banned regions'.
// - If a tower is selected, no other towers from its banned
// region may be selected. Thus, we need to select the
// maximum amount of towers such that none of their banned
// regions overlap.
// - A region may be completely contained in a another. Let us
// ignore this case from now on.
// - Any two regions, that are not contained in each other, will not
// intersect at any point other than an optional shared endpoint,
// and because regions with a shared endpoint may be selected,
// we will have to find the amount of regions when completely
// contained regions are removed.
// - If a region is contained inside another, it is optimal to remove
// the larger only of them. If the ranges are exactly the same,
// we will remove the one with a lower value of h[i].
//
// Ideas for the full solution.
// - Let us first try to solve the problem for a fixed
// value of D.
// - We need to be able to efficiently answer range
// queries, that is, we need to be able find the maximum amount
// non-contained regions in a range.
// - One solution, that is also easily made persistent, is
// to use two segments trees. One for finding the first range,
// and the other for finding the amount of ranges (by for example,
// counting endpoints of ranges in the tree).
// - Now it's pretty easy to generalize this for all values of D.
// - Let us sweep through values of D.
// - An increase in the value of D, can cause some ranges to lengthen.
// - It may happen that when a range lengthens it 'eats' another range
// inside of it, which makes it redundant.
// - Let us start with D = 1 and the largest possible set of
// non-contained ranges.
// - We have to account for the 'eating' of regions, while keeping
// track of the ranges.
// - At any point, the amount of regions in a query range, is the answer
// to the range query.
// - One more important observation. The optimal set of regions forms
// a continuous chain, which makes it easy to maintain as any
// lengthening will cause ranges to 'eat' each other, and thus
// cause a removal.
int n;
vector<int> h;
void init(int n, vector<int> h) {
::n = n;
::h = h;
}
int max_towers(int l, int r, int d) {
r++;
vector<int> region_start(n), region_end(n);
for (int i = l; i < r; ++i) {
region_start[i] = i;
while (region_start[i] >= 0 && h[region_start[i]] < h[i] + d) {
region_start[i]--;
}
region_start[i] = max(region_start[i], l);
region_end[i] = i;
while (region_end[i] < r && h[region_end[i]] < h[i] + d) {
region_end[i]++;
}
region_end[i] = min(region_end[i], r - 1);
}
priority_queue<pair<int, int>> ranges;
for (int i = l; i < r; ++i) {
ranges.emplace(region_start[i] - region_end[i], i);
}
set<pair<int, int>> added;
while (!ranges.empty()) {
int i = ranges.top().second;
ranges.pop();
int l = region_start[i], r = region_end[i];
auto it = added.lower_bound({l, 0});
if (!(it != added.end() && it->second <= r)) {
added.emplace(l, r);
}
}
return added.size();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4088 ms |
2224 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 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 |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
336 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 |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
208 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 |
208 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 |
208 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 |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
3 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
3 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
3 ms |
336 KB |
Output is correct |
31 |
Correct |
3 ms |
336 KB |
Output is correct |
32 |
Correct |
3 ms |
336 KB |
Output is correct |
33 |
Correct |
3 ms |
336 KB |
Output is correct |
34 |
Correct |
3 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 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 |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
336 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 |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
208 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 |
208 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 |
208 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 |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
3 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
3 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
3 ms |
336 KB |
Output is correct |
31 |
Correct |
3 ms |
336 KB |
Output is correct |
32 |
Correct |
3 ms |
336 KB |
Output is correct |
33 |
Correct |
3 ms |
336 KB |
Output is correct |
34 |
Correct |
3 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
30 ms |
1696 KB |
Output is correct |
37 |
Correct |
1431 ms |
2616 KB |
Output is correct |
38 |
Correct |
245 ms |
2108 KB |
Output is correct |
39 |
Correct |
868 ms |
2364 KB |
Output is correct |
40 |
Correct |
66 ms |
2112 KB |
Output is correct |
41 |
Correct |
2644 ms |
3396 KB |
Output is correct |
42 |
Correct |
15 ms |
1808 KB |
Output is correct |
43 |
Correct |
2708 ms |
2752 KB |
Output is correct |
44 |
Correct |
301 ms |
1984 KB |
Output is correct |
45 |
Correct |
40 ms |
1852 KB |
Output is correct |
46 |
Correct |
29 ms |
1852 KB |
Output is correct |
47 |
Correct |
20 ms |
2584 KB |
Output is correct |
48 |
Correct |
26 ms |
2412 KB |
Output is correct |
49 |
Correct |
13 ms |
2068 KB |
Output is correct |
50 |
Correct |
165 ms |
2240 KB |
Output is correct |
51 |
Correct |
1804 ms |
2624 KB |
Output is correct |
52 |
Correct |
52 ms |
4120 KB |
Output is correct |
53 |
Correct |
58 ms |
4880 KB |
Output is correct |
54 |
Correct |
43 ms |
4928 KB |
Output is correct |
55 |
Correct |
3333 ms |
3480 KB |
Output is correct |
56 |
Correct |
2706 ms |
3380 KB |
Output is correct |
57 |
Correct |
3818 ms |
3340 KB |
Output is correct |
58 |
Correct |
2811 ms |
3508 KB |
Output is correct |
59 |
Correct |
1696 ms |
3800 KB |
Output is correct |
60 |
Correct |
107 ms |
4872 KB |
Output is correct |
61 |
Correct |
1507 ms |
4536 KB |
Output is correct |
62 |
Correct |
2612 ms |
4028 KB |
Output is correct |
63 |
Correct |
1378 ms |
4628 KB |
Output is correct |
64 |
Execution timed out |
4059 ms |
1820 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4022 ms |
4256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4061 ms |
1596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 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 |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
336 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 |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
208 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 |
208 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 |
208 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 |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
3 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
3 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
3 ms |
336 KB |
Output is correct |
31 |
Correct |
3 ms |
336 KB |
Output is correct |
32 |
Correct |
3 ms |
336 KB |
Output is correct |
33 |
Correct |
3 ms |
336 KB |
Output is correct |
34 |
Correct |
3 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
30 ms |
1696 KB |
Output is correct |
37 |
Correct |
1431 ms |
2616 KB |
Output is correct |
38 |
Correct |
245 ms |
2108 KB |
Output is correct |
39 |
Correct |
868 ms |
2364 KB |
Output is correct |
40 |
Correct |
66 ms |
2112 KB |
Output is correct |
41 |
Correct |
2644 ms |
3396 KB |
Output is correct |
42 |
Correct |
15 ms |
1808 KB |
Output is correct |
43 |
Correct |
2708 ms |
2752 KB |
Output is correct |
44 |
Correct |
301 ms |
1984 KB |
Output is correct |
45 |
Correct |
40 ms |
1852 KB |
Output is correct |
46 |
Correct |
29 ms |
1852 KB |
Output is correct |
47 |
Correct |
20 ms |
2584 KB |
Output is correct |
48 |
Correct |
26 ms |
2412 KB |
Output is correct |
49 |
Correct |
13 ms |
2068 KB |
Output is correct |
50 |
Correct |
165 ms |
2240 KB |
Output is correct |
51 |
Correct |
1804 ms |
2624 KB |
Output is correct |
52 |
Correct |
52 ms |
4120 KB |
Output is correct |
53 |
Correct |
58 ms |
4880 KB |
Output is correct |
54 |
Correct |
43 ms |
4928 KB |
Output is correct |
55 |
Correct |
3333 ms |
3480 KB |
Output is correct |
56 |
Correct |
2706 ms |
3380 KB |
Output is correct |
57 |
Correct |
3818 ms |
3340 KB |
Output is correct |
58 |
Correct |
2811 ms |
3508 KB |
Output is correct |
59 |
Correct |
1696 ms |
3800 KB |
Output is correct |
60 |
Correct |
107 ms |
4872 KB |
Output is correct |
61 |
Correct |
1507 ms |
4536 KB |
Output is correct |
62 |
Correct |
2612 ms |
4028 KB |
Output is correct |
63 |
Correct |
1378 ms |
4628 KB |
Output is correct |
64 |
Execution timed out |
4059 ms |
1820 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4088 ms |
2224 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |