#include "towers.h"
#include<bits/stdc++.h>
using namespace std;
const int lim = 1e5 + 5;
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
int n, k, log_v[lim], h[lim], spt[lim][17];
void init(int N, vector<int>H) {
n = N;
log_v[0] = -1;
for(int i = 0; i < n; i++){
spt[i + 1][0] = h[i + 1] = H[i];
log_v[i + 1] = log_v[(i + 1) >> 1] + 1;
}
for(int j = 1; j < 17; j++){
for(int i = 1; i + (1 << j) - 1 <= n; i++){
spt[i][j] = max(spt[i][j - 1], spt[i + (1 << (j - 1))][j - 1]);
}
}
}
int get_max(int l, int r){
int k = log_v[r - l + 1];
return max(spt[l][k], spt[r - (1 << k) + 1][k]);
}
int max_towers(int l, int r, int d) {
l++;
r++;
vector<int>dp(n + 1, 0), bit(n + 1, 0);
auto update = [&] (int p, int x){
for(; p <= n; p += p & -p){
maximize(bit[p], x);
}
};
auto get = [&] (int p){
int ans = 0;
for(; p > 0; p -= p & -p){
maximize(ans, bit[p]);
}
return ans;
};
for(int i = l; i <= r; i++){
int low = 1, high = i - 1, p = 0;
while(low <= high){
int mid = (low + high) >> 1;
if(get_max(mid, i - 1) - d >= h[i]){
low = (p = mid) + 1;
}
else{
high = mid - 1;
}
}
dp[i] = get(p) + 1;
low = i + 1;
p = (high = n) + 1;
while(low <= high){
int mid = (low + high) >> 1;
if(get_max(i + 1, mid) - d >= h[i]){
high = (p = mid) - 1;
}
else{
low = mid + 1;
}
}
update(p, dp[i]);
}
return get(n);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |