BZOJ 2216: [Poi2011]Lightning Conductor 决策单调性DP
2017-05-25 20:21
429 查看
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
题解:
这道题因为有绝对值,所以我们选择正反各做一遍去掉绝对值,这道题的决策单调性还是相对比较好证明的,对于j < k 如果a[j]+sqrt(i-j) < a[k]+sqrt(i-k)那么j在之后的决策中一定也不会比k更优,因为sqrt(i)是一个斜率逐渐减小的函数,值越大增加的越慢,剩下的就裸上决策单调DP就可以了。
已知一个长度为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
题解:
这道题因为有绝对值,所以我们选择正反各做一遍去掉绝对值,这道题的决策单调性还是相对比较好证明的,对于j < k 如果a[j]+sqrt(i-j) < a[k]+sqrt(i-k)那么j在之后的决策中一定也不会比k更优,因为sqrt(i)是一个斜率逐渐减小的函数,值越大增加的越慢,剩下的就裸上决策单调DP就可以了。
#include<cstdio> #include<cstdlib> #include<iostream> #include<iomanip> #include<cstring> #include<string> #include<ctime> #include<cmath> #include<algorithm> using namespace std; int stack[600000]; int g[600000]; int n; double calc(int a[],int j,int i) { return (double)a[j]+sqrt(i-j)-a[i]; } int top; int get_pos(int i) { int l=1,r=top; int jilu=0; while(l<=r) { int mid=l+r>>1; if(g[stack[mid]]<=i) jilu=stack[mid],l=mid+1; else r=mid-1; } return jilu; } int erfen(int a[],int i) { int l=max(g[stack[top]]-1,i)+1,r=n; int jilu=n+1; while(l<=r) { int mid=l+r>>1; if(calc(a,stack[top],mid)<calc(a,i,mid)) jilu=mid,r=mid-1; else l=mid+1; } return jilu; } void dp(int a[],double f[]) { top=1; stack[1]=1; g[1]=2; f[1]=-1000000; for(int i=2;i<=n;i++) { int pos=get_pos(i); f[i]=calc(a,pos,i); while(g[stack[top]]>i && calc(a,stack[top],g[stack[top]])<calc(a,i,g[stack[top]])) top--; pos=erfen(a,i); if(pos==n+1) continue; stack[++top]=i; g[i]=pos; } } int a[600000]; int fa[600000]; double f1[600000]; double f2[600000]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); fa[n-i+1]=a[i]; } dp(a,f1); dp(fa,f2); for(int i=1;i<=n;i++) { f1[i]=max(f1[i],f2[n-i+1]); printf("%d\n",max(0,int(ceil(f1[i])))); } return 0; }
相关文章推荐
- 【BZOJ 2216】【POI 2011】[动态规划][决策点单调优化]Lightning Conductor
- bzoj 2216: [Poi2011]Lightning Conductor(DP决策单调性)
- BZOJ2216 [Poi2011]Lightning Conductor 【决策单调性dp】
- [BZOJ 2216][Poi2011]Lightning Conductor:DP决策单调性
- bzoj 2216: [Poi2011]Lightning Conductor【决策单调性dp+分治】
- BZOJ_2216_[Poi2011]Lightning Conductor_决策单调性
- bzoj2216: [Poi2011]Lightning Conductor 决策单调性
- [BZOJ2216][Poi2011]Lightning Conductor(dp+决策单调性)
- bzoj 2216: [Poi2011]Lightning Conductor 决策单调性+cdq分治
- 【BZOJ2216】[Poi2011]Lightning Conductor 决策单调性
- bzoj 2216 [Poi2011]Lightning Conductor 决策单调性
- bzoj2216: [Poi2011]Lightning Conductor(分治决策单调性优化)
- 2216: [Poi2011]Lightning Conductor DP+决策单调性
- Bzoj:[Poi2011]Lightning Conductor:决策单调性优化DP详解
- 【bzoj2216】[Poi2011]Lightning Conductor 决策单调性+整体二分
- bzoj2216 [Poi2011]Lightning Conductor(决策单调性DP)
- bzoj 2216: [Poi2011]Lightning Conductor
- BZOJ 2442[Usaco2011 Open] 修剪草坪 (dp+单调队列)
- BZOJ2276 [Poi2011]Temperature 【单调队列】
- 【BZOJ】2216: [Poi2011]Lightning Conductor