题解 P5503 【[JSOI2016]灯塔】
Rorschachindark · · 题解
题目传送门
题目大意
给出一个长度为
思路
都 0202 年了,为什么题解里面还是没有决策单调栈的做法啊?
首先我们发现,我们可以把
然后我们发现其实这个式子可以换成如下形式:
于是问题就是如何求到
不过话说这道题
\texttt{Code}
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define MAXN 100005
int n,head,tail,h[MAXN];
double res1[MAXN],res2[MAXN];
struct node{
int l,r,x;
}sta[MAXN];
double contri (int i,int j){return h[j] + sqrt (i - j);}
int bound (int i,int j){//找到i比j更优的最后一个点
int l = max (i,j),r = n,ans = 0;
while (l <= r){
int mid = (l + r) >> 1;
if (contri (mid,i) >= contri (mid,j)) ans = mid,l = mid + 1;
else r = mid - 1;
}
return ans;
}
void Solve (double *p1){
sta[head = tail = 1] = node {1,n,1};
for (Int i = 2;i <= n;++ i){
while (head < tail && sta[head].r < i) ++ head;
p1[i] = contri (i,sta[head].x);
if (contri (n,i) <= contri (n,sta[tail].x)) continue;
while (head < tail && contri (sta[tail].l,sta[tail].x) <= contri (sta[tail].l,i)) -- tail;
sta[tail].r = bound (sta[tail].x,i),sta[++ tail] = node {sta[tail - 1].r + 1,n,i};
}
}
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline T Min (T a,T b){return a > b ? b : a;}
signed main(){
read (n);
for (Int i = 1;i <= n;++ i) read (h[i]);
Solve (res1);reverse (h + 1,h + n + 1);Solve (res2);reverse (h + 1,h + n + 1);
for (Int i = 1,tmp;i <= n;++ i) tmp = (int)ceil (max (res1[i],res2[n - i + 1])) - h[i],write (max (tmp,0)),putchar ('\n');
return 0;
}