#include <algorithm>
#include <map>
#include <memory>
#include <optional>
#include <queue>
#include <unordered_set>
#include <vector>
int n;
std::vector<int> h;
std::map<int, int> data;
void init(int N, std::vector<int> H) {
n = N;
h = H;
int towers = 0;
struct D {
int prev;
int max_to_prev;
int next;
int max_to_next;
};
std::vector<D> adj;
for (int i = 0; i < N; i++) {
towers += 1;
int max_to_prev = H[i];
int max_to_next = H[i];
if (i != 0) max_to_prev = std::max(max_to_prev, H[i - 1]);
if (i + 1 != N) max_to_next = std::max(max_to_next, H[i + 1]);
adj.push_back({.prev = i - 1,
.max_to_prev = max_to_prev,
.next = i + 1 == N ? -1 : i + 1,
.max_to_next = max_to_next});
}
struct PQItem {
int priority;
int from;
int to;
bool operator<(const PQItem &rhs) const {
return priority > rhs.priority;
}
};
std::vector<bool> active(adj.size(), true);
std::priority_queue<PQItem> pq;
for (int i = 1; i < adj.size(); i++) {
pq.push({.priority = 0, .from = i - 1, .to = i});
}
while (!pq.empty()) {
auto [priority, from, to] = pq.top();
pq.pop();
if (!active[from] || !active[to]) continue;
data.emplace(priority, towers);
if (H[from] > H[to]) {
int max = adj[to].max_to_prev =
std::max(adj[from].max_to_prev, adj[to].max_to_prev);
adj[to].prev = adj[from].prev;
if (adj[to].prev != -1) {
adj[adj[to].prev].next = to;
pq.push({.priority = max - std::max(H[adj[to].prev], H[to]),
.from = adj[to].prev,
.to = to});
}
towers -= 1;
active[from] = false;
} else {
int max = adj[from].max_to_next =
std::max(adj[from].max_to_next, adj[to].max_to_next);
adj[from].next = adj[to].next;
if (adj[from].next != -1) {
adj[adj[from].next].prev = from;
pq.push({.priority = max - std::max(H[from], H[adj[from].next]),
.from = from,
.to = adj[from].next});
}
towers -= 1;
active[to] = false;
}
}
data.insert_or_assign(2'000'000'000, towers);
}
int max_towers(int L, int R, int D) {
if (L == R) return 1;
R += 1;
return data.lower_bound(D)->second;
}
Compilation message
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<init(int, std::vector<int>)::D>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 1; i < adj.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
282 ms |
3328 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
521 ms |
5632 KB |
1st lines differ - on the 1st token, expected: '11903', found: '33010' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
1520 KB |
Output is correct |
2 |
Correct |
692 ms |
5764 KB |
Output is correct |
3 |
Correct |
600 ms |
5752 KB |
Output is correct |
4 |
Correct |
638 ms |
6520 KB |
Output is correct |
5 |
Correct |
735 ms |
6524 KB |
Output is correct |
6 |
Correct |
751 ms |
6536 KB |
Output is correct |
7 |
Correct |
808 ms |
6496 KB |
Output is correct |
8 |
Correct |
587 ms |
5772 KB |
Output is correct |
9 |
Correct |
703 ms |
5696 KB |
Output is correct |
10 |
Correct |
784 ms |
5652 KB |
Output is correct |
11 |
Correct |
755 ms |
5748 KB |
Output is correct |
12 |
Correct |
35 ms |
5760 KB |
Output is correct |
13 |
Correct |
38 ms |
6528 KB |
Output is correct |
14 |
Correct |
38 ms |
6516 KB |
Output is correct |
15 |
Correct |
28 ms |
5772 KB |
Output is correct |
16 |
Correct |
29 ms |
5640 KB |
Output is correct |
17 |
Correct |
41 ms |
5632 KB |
Output is correct |
18 |
Correct |
35 ms |
5768 KB |
Output is correct |
19 |
Correct |
35 ms |
5748 KB |
Output is correct |
20 |
Correct |
38 ms |
6460 KB |
Output is correct |
21 |
Correct |
38 ms |
6524 KB |
Output is correct |
22 |
Correct |
39 ms |
6528 KB |
Output is correct |
23 |
Correct |
38 ms |
6512 KB |
Output is correct |
24 |
Correct |
22 ms |
5696 KB |
Output is correct |
25 |
Correct |
27 ms |
5760 KB |
Output is correct |
26 |
Correct |
28 ms |
5656 KB |
Output is correct |
27 |
Correct |
31 ms |
5768 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 |
0 ms |
208 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
1st lines differ - on the 1st token, expected: '13', found: '131' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
282 ms |
3328 KB |
1st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |