BZOJ 2216 Lightning Conductor

Posted on 2016-09-21 19:06  ziliuziliu  阅读(140)  评论(0编辑  收藏  举报

决策单调。整体二分。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 500500
using namespace std;
int n,a[maxn],f[maxn],g[maxn];
void dp1(int left,int right,int l,int r)
{
    if (left>right) return;
    int mid=left+right>>1;
    int pos;double mx=0;
    for (int i=l;i<=r && i<=mid;i++)
    {
        if ((double)a[i]+sqrt(mid-i)>mx)
        {
            mx=(double)a[i]+sqrt(mid-i);
            pos=i;
        }
    }
    f[mid]=a[pos]+ceil(sqrt(mid-pos));
    dp1(left,mid-1,l,pos);dp1(mid+1,right,pos,r);
}
void dp2(int left,int right,int l,int r)
{
    if (left>right) return;
    int mid=left+right>>1;
    int pos;double mx=0;
    for (int i=r;i>=l && i>=mid;i--)
    {
        if ((double)a[i]+sqrt(i-mid)>mx)
        {
            mx=(double)a[i]+sqrt(i-mid);
            pos=i;
        }
    }
    g[mid]=a[pos]+ceil(sqrt(pos-mid));
    dp2(left,mid-1,l,pos);dp2(mid+1,right,pos,r);
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    dp1(1,n,1,n);dp2(1,n,1,n);
    for (int i=1;i<=n;i++)
        printf("%d\n",max(f[i],g[i])-a[i]);
    return 0;
}