This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template<class T> void chmax(T& a, T b){ if(a < b) a = b; }
int N;
vector<int> H;
vector<pair<int, int>> seg;
void init(int N, vector<int> H) {
::N = N;
::H = H;
seg.resize(N * 2);
for(int i = 0; i < N; i++) seg[N + i] = {H[i], i};
for(int i = N; --i; ){
seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}
}
int argmax(int L, int R){
L += N;
R += N;
pair<int, int> ans;
while(L < R){
if(L & 1) chmax(ans, seg[L++]);
if(R & 1) chmax(ans, seg[--R]);
L >>= 1; R >>= 1;
}
return ans.second;
}
int max_towers(int L, int R, int D) {
auto dfs = [&](int L, int R, auto dfs) -> pair<int, int> {
if(R - L == 0) return {0, INT_MAX};
const int mid = argmax(L, R);
const int x = H[mid];
auto [c1, mn1] = dfs(L, mid, dfs);
auto [c2, mn2] = dfs(mid + 1, R, dfs);
if(c1 <= 1 && c2 <= 1){
if(x - max(mn1, mn2) >= D) return {2, 0};
return {1, min({mn1, mn2, x})};
}
if(c1 < c2){
swap(c1, c2);
swap(mn1, mn2);
}
if(c2 <= 1) return {c1 + (x - mn2 >= D), 0};
return {c1 + c2, 0};
};
return dfs(L, R + 1, dfs).first;
}
# | 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... |