#include "towers.h"
#include <vector>
#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
const int M = 1e5+7;
vector<int> a;
ll getlog[M];
ll n, Log;
ll d;
pair<ll,ll> sp[M][20], sp2[M][20];
pair<ll,ll> get_max(ll l , ll r)
{
ll dif = r-l+1;
ll my = getlog[dif];
return max(sp[l][my], sp[r-(1<<my)+1][my]);
}
pair<ll,ll> get_min(ll l , ll r)
{
ll dif = r-l+1;
ll my = getlog[dif];
return min(sp2[l][my], sp2[r-(1<<my)+1][my]);
}
vector<pair<ll,ll> >v;
ll go(ll l, ll r, ll mxh)
{
if(r<l) return 0;
ll biggest = get_max(l,r).second;
ll new_mxh = min(mxh,a[biggest]-d);
if(l==r) return 1;
if(biggest==l)
{
ll mynum = new_mxh-get_min(biggest+1,r).first+1;
v.push_back({mynum,0});
}
else if(biggest == r)
{
ll mynum = new_mxh - get_min(l,biggest-1).first+1;
v.push_back({mynum,0});
}
else
{
ll mynum1 = new_mxh - get_min(l,biggest-1).first+1;
ll mynum2 = new_mxh - get_min(biggest+1,r).first+1;
if(mynum1>mynum2) v.push_back({mynum1,1}), v.push_back({mynum2,0});
else v.push_back({mynum1,0}), v.push_back({mynum2,1});
}
return max(1LL,go(l,biggest-1,new_mxh) + go(biggest+1,r,new_mxh));
}
ll bestans;
void init(int N, std::vector<int> H) {
n = N; a = H;
for(int i = 0; i < n; i++) sp[i][0] = sp2[i][0] = {a[i],i};
Log = log2(n)+1;
// cout << Log << endl;
for(int j = 1; j < Log; j++)
{
for(int i = 0; i < n; i++)
{
if(i+(1<<j)-1<n) sp[i][j] = max(sp[i][j-1],sp[i+(1<<j-1)][j-1]);
if(i+(1<<j)-1<n) sp2[i][j] = min(sp2[i][j-1],sp2[i+(1<<j-1)][j-1]);
}
}
getlog[0] = -1;
for(int i = 1; i <= n; i++) getlog[i] = getlog[i/2]+1;
bestans = go(0,n-1,1e18);
sort(v.begin(), v.end());
// cout << bestans << endl;
// for(int i = 0; i <(int) v.size(); i++) cout << v[i].first << " " << v[i].second << endl;
for(int i = v.size()-1; i >= 1; i--)
{
if(v[i-1].F==v[i].F) v[i-1].S+=v[i].S,v[i].S = 0;
}
for(int i = 1; i < v.size(); i++) v[i].S += v[i-1].S;
// for(int i = 0; i < v.size(); i++) cout << v[i].F << " "; cout << endl;
}
int max_towers(int L, int R, int D) {
auto it = upper_bound(v.begin(),v.end(),pair<ll,ll> (D,1e18));
if(it==v.begin()) return bestans;
it--;
return bestans - (*it).S;
}
# | 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... |