【POI2011】【BZOJ2216】Lightning Conductor

Description

已知一个长度为n的序列a1,a2,…,an。
对于每个1<=i<=n,找到最小的非负整数p满足 对于任意的j, aj < = ai + p - sqrt(abs(i-j))

Input

第一行n,(1<=n<=500000)
下面每行一个整数,其中第i行是ai。(0<=ai<=1000000000)

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]))));
}


你可能感兴趣的:(dp,决策单调)