# 【POI2011】【BZOJ2216】Lightning Conductor

Description

Input

Output

n行，第i行表示对于i，得到的p

Sample Input

6

5

3

2

4

2

4

Sample Output

2

3

5

3

5

4

HINT

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define MAXN 500010
#define GET (ch>='0'&&ch<='9')
using namespace std;
template <class classname>
inline void in(classname &x)
{
char ch=getchar();x=0;
while (!GET)    ch=getchar();
while (GET) x=x*10+ch-'0',ch=getchar();
}
int n;
int a[MAXN];
double f[2][MAXN];
struct node
{
int l,r,x;
node()  {}
node(int _l,int _r,int _x)  {   l=_l;r=_r;x=_x; }
}q[MAXN];
inline double calc(int j,int i) {   return a[j]+sqrt(abs(j-i))-a[i];    }
inline int find(node d,int x)
{
int l=d.l,r=d.r,mid=(l+r)>>1;
for (;l<=r;mid=(l+r)>>1)   if (calc(x,mid)<calc(d.x,mid))   l=mid+1;    else    r=mid-1;
return l;
}
void solve(int id)
{
for (int i=1,h=1,t=0,j;i<=n;i++)
{
if (h<=t&&++q[h].l>q[h].r)    h++;f[id][i]=calc(q[h].x,i);
if (h>t||calc(i,n)>calc(q[t].x,n))
{
while (h<=t&&calc(i,q[t].l)>calc(q[t].x,q[t].l))  t--;
if (h<=t)    j=find(q[t],i),q[t].r=j-1,q[++t]=node(j,n,i);
else    q[++t]=node(i,n,i);
}
}
}
int main()
{
in(n);
for (int i=1;i<=n;i++)   in(a[i]);
solve(0);
for (int i=1;i<=(n>>1);i++)    swap(a[i],a[n-i+1]);
solve(1);
for (int i=1;i<=n;i++)   printf("%d\n",max(0,(int)ceil(max(f[0][i],f[1][n-i+1]))));
}
﻿