高效高效
分块。
here
#include<bits/stdc++.h>
using namespace std;
int n,a[500010];
double dp[500010],sqr[500010];
double w(int j,int i) {
return double(a[j])+sqr[i-j];
}
void work(int l,int r,int L,int R) {
if(l>r) return ;
int mid=l+r>>1,p;
double maxn=0;
for(int i=L; i<=min(mid,R); i++) {
if(w(i,mid)>maxn) maxn=w(i,mid),p=i;
}
dp[mid]=max(dp[mid],maxn);
work(l,mid-1,L,p);
work(mid+1,r,p,R);
}
int main() {
cin>>n;
for(int i=1; i<=n; i++) {
cin>>a[i];
sqr[i]=sqrt(i);
}
work(1,n,1,n);
for(int i=1; i<=n/2; i++) swap(a[i],a[n-i+1]),swap(dp[i],dp[n-i+1]);
work(1,n,1,n);
for(int i=n; i>=1; i--) cout<<(int)ceil(dp[i])-a[i]<<endl;
return 0;
}
标签:int,double,work,mid,maxn,sqr,P3515
From: https://www.cnblogs.com/zan-mei-tai-yang/p/18408842