斜率优化DP——BZOJ1010/Luogu3195 [HNOI2008]玩具装箱TOY
来源:互联网 发布:linux ftp命令登录 编辑:程序博客网 时间:2024/05/23 07:23
题面:Luogu3195 BZOJ1010
本来以为斜率优化是个什么高级东西。。。这题入门之后……
发现也没什么难的吧
转移:
我们考虑怎么把上面的
显然,如果
其中
我们把
化开来是:
同除以
然后发现这个不等式化成了一个斜率不等式
所以我们可以斜率优化这个dp,其实就是维护一个下凸壳
每次把斜率大于s[i]的最小答案来转移,最后把不是下凸壳的节点删掉
用单调队列维护一下就好了
#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <iostream>#include <ctime>#include <map>#include <queue>#include <cstdlib>#include <string>#include <climits>#include <set>#include <vector>#define int long longusing namespace std;int n,L,a[1000001],q[1000001],s[1000001],f[1000001]={0};inline int sqr(int x){return x*x;}inline double check(int x,int y){ return (double)((f[x]+sqr(s[x]+L)-f[y]-sqr(s[y]+L))/(2.0*(s[x]-s[y])));}signed main(){ scanf("%lld%lld",&n,&L);L++; for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); a[i]+=a[i-1];s[i]=a[i]+i; } int l=1,r=1;q[1]=0; for(int i=1;i<=n;i++){ while(l<r&&check(q[l+1],q[l])<s[i]+1.0)l++; f[i]=f[q[l]]+sqr(s[i]-s[q[l]]-L); while(l<r&&check(q[r],q[r-1])>check(i,q[r]))r--; q[++r]=i; } printf("%lld",f[n]); return 0;}
阅读全文
1 0
- 斜率优化DP——BZOJ1010/Luogu3195 [HNOI2008]玩具装箱TOY
- bzoj1010: [HNOI2008]玩具装箱toy 斜率优化DP
- BZOJ1010 [HNOI2008]玩具装箱toy(斜率优化dp)
- 【DP+斜率优化】[HNOI2008][HYSBZ/BZOJ1010]玩具装箱toy
- 【bzoj1010】[HNOI2008]玩具装箱toy 斜率优化DP
- [BZOJ1010][HNOI2008]玩具装箱toy(斜率优化dp)
- 斜率优化dp专题 & BZOJ1010 HNOI2008 玩具装箱toy
- BZOJ1010: [HNOI2008]玩具装箱toy 斜率优化DP
- 【BZOJ1010】【HNOI2008】玩具装箱toy(dp+斜率优化)
- [bzoj1010][HNOI2008] 玩具装箱toy DP斜率优化
- 【斜率优化DP】BZOJ1010 [HNOI2008]玩具装箱toy
- bzoj1010 [HNOI2008]玩具装箱toy 斜率优化 DP
- bzoj1010 [HNOI2008]玩具装箱toy(斜率优化DP)
- bzoj1010[HNOI2008]玩具装箱toy 斜率优化dp
- BZOJ1010(HNOI2008)[玩具装箱toy]--斜率优化DP
- 【bzoj1010】【斜率优化】【HNOI2008】玩具装箱toy
- [HNOI2008][BZOJ1010] 玩具装箱toy - 斜率优化
- 【bzoj1010】【HNOI2008】【玩具装箱toy】【斜率优化】
- 大话设计模式——策略模式
- JS基础和JQ点滴
- 刘强东:第四次零售革命意义将超互联网
- 杨辉三角快速幂分治组合数
- 工薪层每年存款翻一番,2年后自己做老板
- 斜率优化DP——BZOJ1010/Luogu3195 [HNOI2008]玩具装箱TOY
- 面向切面编程(一)
- 单点更新 区间查询最值 线段树 杭电hdu1754
- 博弈搜索练习-BFS&DFS
- Struts2开发笔记一
- [codevs1222]信与信封问题(匈牙利)
- 声明!!!!!!!!!!!!!!!!!!!
- JS(初学)
- Kotlin-类型安全的建设者