#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
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 = 2e9;
if (l_id[i] != -1 && l_id[i] != i - 1)
d = min(d, sparse::query(l_id[i], i) - h[i]);
if (r_id[i] != -1 && r_id[i] != i + 1)
d = min(d, sparse::query(i, r_id[i]) - h[i]);
// 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 = -1;
while (l <= r) {
int mid = l + r >> 1;
if (vec[mid][0] <= x)
l = mid + 1, p = mid;
else
r = mid - 1;
}
return p + 1;
}
int max_towers(int l, int r, int d) {
return find_first(d) + 1;
}
/*
7 1
10 20 60 40 50 30 70
0 6 17
*/
Compilation message
towers.cpp: In function 'int find_first(int)':
towers.cpp:68:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
68 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
450 ms |
12048 KB |
1st lines differ - on the 1st token, expected: '1', found: '15799' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '31' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '31' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
736 ms |
19600 KB |
1st lines differ - on the 1st token, expected: '11903', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
313 ms |
4876 KB |
1st lines differ - on the 1st token, expected: '7197', found: '2392' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '31' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
450 ms |
12048 KB |
1st lines differ - on the 1st token, expected: '1', found: '15799' |
2 |
Halted |
0 ms |
0 KB |
- |