首页 > 代码库 > BZOJ 2216 Lightning Conductor
BZOJ 2216 Lightning Conductor
决策单调。整体二分。
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#define maxn 500500using 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;}
BZOJ 2216 Lightning Conductor
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。