[Poi2011]Lightning Conductor

[Poi2011]Lightning Conductor

决策单调性,分治求解(当然也可以单调队列维护)

单调性:对于每一个\(i\),先考虑左边的决策点\(j\),则\(j\)\(i\)的递增而递增

意会型证明:

如果左边有多个值递减的点,当\(i\)较小时,\(i-j\)较小,\(\sqrt{i-j}\)的梯度较大,所以远处的点可能答案更大,随着\(i\)的增大

\(\sqrt{i-j}\)的梯度减小了,较近的\(j\)就可能有贡献

分治左右两边即可

const int N=5e5+10;
const ll INF=1ll<<60;
 
 
int n,m;
int a[N];
int ans[N];
 
void Solve(int l,int r,int L,int R){ 
    if(l>r) return;
    int mid=(l+r)>>1,id=L;
    double ma=0;
    rep(i,max(L,mid),R) {
        double x=sqrt(i-mid)+a[i];
        if(x>ma) ma=x,id=i;
    }
    ans[mid]=max(ans[mid],(int)ceil(ma)-a[mid]);
    Solve(l,mid-1,L,id);
    Solve(mid+1,r,id,R);
}
 
 
 
 
int main() {
    rep(i,1,n=rd()) a[i]=rd();
    Solve(1,n,1,n);
    rep(i,1,n/2) swap(a[i],a[n-i+1]),swap(ans[i],ans[n-i+1]);
    Solve(1,n,1,n);
    rep(i,1,n) printf("%d\n",ans[n-i+1]);
}
 
posted @ 2019-10-26 15:16  chasedeath  阅读(186)  评论(0编辑  收藏  举报