#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<vector<int>> vec;
namespace sparse {
int n;
vector<int> logs;
vector<vector<int>> st;
void build(vector<int> h) {
n = (int) h.size();
logs.resize(n + 1);
st.resize(n);
for (int i = 0; i < n; i++) {
st[i].resize(20);
st[i][0] = h[i];
}
for (int j = 1; j < 20; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
for (int i = 2; i <= n; i++) logs[i] = logs[i >> 1] + 1;
}
int query(int l, int r) {
int k = logs[r - l + 1];
return max(st[l][k], st[r - (1 << k) + 1][k]);
}
};
void init(int n, vector<int> h) {
vector<int> l_id(n), r_id(n);
vector<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && h[i] < h[stk.back()]) {
stk.pop_back();
}
l_id[i] = (stk.empty() ? -1 : stk.back());
stk.push_back(i);
}
stk.clear();
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && h[i] < h[stk.back()]) {
stk.pop_back();
}
r_id[i] = (stk.empty() ? -1 : stk.back());
stk.push_back(i);
}
sparse::build(h);
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int i, int j) { return h[i] < h[j]; });
for (int _i = 1; _i < n; _i++) {
int i = order[_i];
int d = 1e9;
if (l_id[i] != -1)
d = min(d, sparse::query(l_id[i], i) - h[i]);
if (r_id[i] != -1)
d = min(d, sparse::query(i, r_id[i]) - h[i]);
// assert(d != 0);
// printf("%d %d\n", i + 1, d);
vec.push_back({d, i, l_id[i], r_id[i]});
}
sort(vec.begin(), vec.end());
}
int find_first(int x) {
int l = 0, r = vec.size() - 1, p = vec.size();
while (l <= r) {
int mid = l + r >> 1;
if (vec[mid][0] >= x)
r = mid - 1, p = mid;
else
l = mid + 1;
}
return vec.size() - p;
}
int get(int i) { return sparse::query(i, i); }
bool taken[N];
int max_towers(int l, int r, int d) {
vector<int> ids;
for (auto& p : vec)
if (p[0] >= d && l <= p[1] && p[1] <= r)
ids.push_back(p[1]), taken[p[1]] = true;
sort(ids.begin(), ids.end());
int idx = l;
for (int i = l; i <= r; i++) {
if (get(i) < get(idx)) {
idx = i;
}
}
function<void(int)> Try = [&](int idx) {
int L = l - 1, R = r + 1;
int ans = ids.size();
for (int i : ids) {
if (i < idx)
L = max(L, i);
else
R = min(R, i);
}
bool ok = true;
if (L >= l && max(get(L), get(idx)) > sparse::query(L, idx) - d)
ok = false;
if (R <= r && max(get(R), get(idx)) > sparse::query(idx, R) - d)
ok = false;
if (ok)
ids.push_back(idx);
};
for (int i = l; i <= r; i++)
if (!taken[i])
Try(i);
for (int i : ids)
taken[i] = false;
return ids.size();
}
/*
7 3
10 20 60 40 50 30 70
1 5 10
2 2 100
0 6 17
*/
Compilation message
towers.cpp: In function 'int find_first(int)':
towers.cpp:70:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | int mid = l + r >> 1;
| ~~^~~
towers.cpp: In lambda function:
towers.cpp:96:13: warning: unused variable 'ans' [-Wunused-variable]
96 | int ans = ids.size();
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4056 ms |
12112 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
592 KB |
Output is correct |
20 |
Correct |
3 ms |
592 KB |
Output is correct |
21 |
Correct |
3 ms |
592 KB |
Output is correct |
22 |
Correct |
3 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
3 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
592 KB |
Output is correct |
20 |
Correct |
3 ms |
592 KB |
Output is correct |
21 |
Correct |
3 ms |
592 KB |
Output is correct |
22 |
Correct |
3 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
3 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
118 ms |
12972 KB |
Output is correct |
37 |
Correct |
188 ms |
19752 KB |
Output is correct |
38 |
Correct |
116 ms |
19768 KB |
Output is correct |
39 |
Correct |
115 ms |
19856 KB |
Output is correct |
40 |
Correct |
124 ms |
19856 KB |
Output is correct |
41 |
Correct |
1305 ms |
19768 KB |
Output is correct |
42 |
Correct |
92 ms |
19772 KB |
Output is correct |
43 |
Correct |
58 ms |
20208 KB |
Output is correct |
44 |
Correct |
48 ms |
20248 KB |
Output is correct |
45 |
Correct |
59 ms |
19996 KB |
Output is correct |
46 |
Correct |
58 ms |
20012 KB |
Output is correct |
47 |
Correct |
357 ms |
19832 KB |
Output is correct |
48 |
Correct |
203 ms |
19768 KB |
Output is correct |
49 |
Correct |
98 ms |
19768 KB |
Output is correct |
50 |
Correct |
56 ms |
20232 KB |
Output is correct |
51 |
Correct |
66 ms |
20272 KB |
Output is correct |
52 |
Correct |
2285 ms |
19864 KB |
Output is correct |
53 |
Correct |
2549 ms |
19768 KB |
Output is correct |
54 |
Correct |
2541 ms |
19752 KB |
Output is correct |
55 |
Correct |
53 ms |
20272 KB |
Output is correct |
56 |
Correct |
72 ms |
20072 KB |
Output is correct |
57 |
Correct |
1077 ms |
19156 KB |
Output is correct |
58 |
Correct |
1600 ms |
19768 KB |
Output is correct |
59 |
Correct |
1928 ms |
19872 KB |
Output is correct |
60 |
Correct |
2544 ms |
19768 KB |
Output is correct |
61 |
Correct |
2471 ms |
19856 KB |
Output is correct |
62 |
Correct |
2097 ms |
19880 KB |
Output is correct |
63 |
Correct |
2482 ms |
19836 KB |
Output is correct |
64 |
Correct |
52 ms |
20272 KB |
Output is correct |
65 |
Correct |
57 ms |
20392 KB |
Output is correct |
66 |
Correct |
64 ms |
20120 KB |
Output is correct |
67 |
Correct |
53 ms |
20172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4022 ms |
19732 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4018 ms |
4992 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
592 KB |
Output is correct |
20 |
Correct |
3 ms |
592 KB |
Output is correct |
21 |
Correct |
3 ms |
592 KB |
Output is correct |
22 |
Correct |
3 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
3 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
592 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
118 ms |
12972 KB |
Output is correct |
37 |
Correct |
188 ms |
19752 KB |
Output is correct |
38 |
Correct |
116 ms |
19768 KB |
Output is correct |
39 |
Correct |
115 ms |
19856 KB |
Output is correct |
40 |
Correct |
124 ms |
19856 KB |
Output is correct |
41 |
Correct |
1305 ms |
19768 KB |
Output is correct |
42 |
Correct |
92 ms |
19772 KB |
Output is correct |
43 |
Correct |
58 ms |
20208 KB |
Output is correct |
44 |
Correct |
48 ms |
20248 KB |
Output is correct |
45 |
Correct |
59 ms |
19996 KB |
Output is correct |
46 |
Correct |
58 ms |
20012 KB |
Output is correct |
47 |
Correct |
357 ms |
19832 KB |
Output is correct |
48 |
Correct |
203 ms |
19768 KB |
Output is correct |
49 |
Correct |
98 ms |
19768 KB |
Output is correct |
50 |
Correct |
56 ms |
20232 KB |
Output is correct |
51 |
Correct |
66 ms |
20272 KB |
Output is correct |
52 |
Correct |
2285 ms |
19864 KB |
Output is correct |
53 |
Correct |
2549 ms |
19768 KB |
Output is correct |
54 |
Correct |
2541 ms |
19752 KB |
Output is correct |
55 |
Correct |
53 ms |
20272 KB |
Output is correct |
56 |
Correct |
72 ms |
20072 KB |
Output is correct |
57 |
Correct |
1077 ms |
19156 KB |
Output is correct |
58 |
Correct |
1600 ms |
19768 KB |
Output is correct |
59 |
Correct |
1928 ms |
19872 KB |
Output is correct |
60 |
Correct |
2544 ms |
19768 KB |
Output is correct |
61 |
Correct |
2471 ms |
19856 KB |
Output is correct |
62 |
Correct |
2097 ms |
19880 KB |
Output is correct |
63 |
Correct |
2482 ms |
19836 KB |
Output is correct |
64 |
Correct |
52 ms |
20272 KB |
Output is correct |
65 |
Correct |
57 ms |
20392 KB |
Output is correct |
66 |
Correct |
64 ms |
20120 KB |
Output is correct |
67 |
Correct |
53 ms |
20172 KB |
Output is correct |
68 |
Execution timed out |
4022 ms |
19732 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4056 ms |
12112 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |