[BZOJ1010][HNOI2008]玩具装箱(决策单调性/斜率优化DP)
来源:互联网 发布:linux 退出nano 编辑:程序博客网 时间:2024/04/28 01:52
容易推出,如果玩具长度的前缀和为
解法一:决策单调性。
可以看出原方程是一个
设
此时
可得
决策单调性实现即,用一个栈维护每个决策的起始和结束位置。对于每个已经计算出来的
具体为:
(1)在栈顶的决策起始位置判断起始位置是否决策
(2)二分查找在栈顶的决策区间里,决策
代码:
#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;inline int read() { int res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-'); if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + (c - 48); return bo ? ~res + 1 : res;}typedef long long ll;const int N = 5e4 + 5;int n, L, c[N], top; ll sm[N], f[N];struct cyx { int l, r, x; cyx() {} cyx(int _l, int _r, int _x) : l(_l), r(_r), x(_x) {}} stk[N];ll w(int i, int j) { ll res = sm[j] - sm[i] - L; res += j - i - 1; return res * res;}int findx(int i) { int l = stk[top].l, r = stk[top].r, mid; while (l <= r) { // 二分查找新决策的起始位置 mid = l + r >> 1; if (f[i] + w(i, mid) < f[stk[top].x] + w(stk[top].x, mid)) r = mid - 1; else l = mid + 1; } return l;}int main() { int i, now = 1; n = read(); L = read(); for (i = 1; i <= n; i++) c[i] = read(), sm[i] = sm[i - 1] + c[i]; stk[top = 1] = cyx(1, n, 0); for (i = 1; i <= n; i++) { f[i] = f[stk[now].x] + w(stk[now].x, i); while (i < stk[top].l && f[i] + w(i, stk[top].l) < f[stk[top].x] + w(stk[top].x, stk[top].l)) top--; int u = findx(i); stk[top].r = u - 1; if (u <= n) stk[++top] = cyx(u, n, i); if (i == stk[now].r) now++; } cout << f[n] << endl; return 0;}
时间复杂度
解法二:斜率优化。
可以发现原方程为
设
则
即
再设
则可以发现
代码:
#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;inline int read() { int res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-'); if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + (c - 48); return bo ? ~res + 1 : res;}typedef long long ll;const int N = 5e4 + 5;int n, H, T, L, que[N];ll f[N], a[N], b[N], X[N], Y[N];bool check(int p1, int p2, int p3) { return (X[p2] - X[p3]) * (Y[p1] - Y[p3]) - (X[p1] - X[p3]) * (Y[p2] - Y[p3]) >= 0; // 根据叉积判断p1p2的斜率是否大于p2p3的斜率}ll calc(int x, int y) { return f[x] + b[x] * b[x] - 2ll * a[y] * b[x];}int main() { int i; n = read(); L = read(); H = T = 1; for (i = 1; i <= n; i++) { b[i] = b[i - 1] + read() + 1; a[i] = b[i] - L - 1; } for (i = 1; i <= n; i++) { while (H < T && calc(que[H], i) >= calc(que[H + 1], i)) H++; f[i] = calc(que[H], i) + a[i] * a[i]; X[i] = b[i]; Y[i] = f[i] + b[i] * b[i]; while (H < T && check(que[T - 1], que[T], i)) T--; que[++T] = i; } cout << f[n] << endl; return 0;}
时间复杂度
- [BZOJ1010][HNOI2008]玩具装箱(决策单调性/斜率优化DP)
- bzoj1010 玩具装箱 【决策单调性优化dp】
- [BZOJ1010]玩具装箱:DP决策单调性
- 1010: [HNOI2008]玩具装箱toy DP+斜率优化+决策单调性
- [BZOJ1010]HNOI2008玩具装箱|斜率优化DP
- BZOJ1010 [HNOI2008]玩具装箱toy(斜率优化dp)
- [BZOJ1010][HNOI2008]玩具装箱toy(斜率优化dp)
- 【BZOJ1010】【HNOI2008】玩具装箱toy(dp+斜率优化)
- bzoj1010 [HNOI2008]玩具装箱toy(斜率优化DP)
- bzoj1010: [HNOI2008]玩具装箱toy 斜率优化DP
- 【DP+斜率优化】[HNOI2008][HYSBZ/BZOJ1010]玩具装箱toy
- 【bzoj1010】[HNOI2008]玩具装箱toy 斜率优化DP
- 斜率优化dp专题 & BZOJ1010 HNOI2008 玩具装箱toy
- BZOJ1010: [HNOI2008]玩具装箱toy 斜率优化DP
- [bzoj1010][HNOI2008] 玩具装箱toy DP斜率优化
- 【斜率优化DP】BZOJ1010 [HNOI2008]玩具装箱toy
- bzoj1010 [HNOI2008]玩具装箱toy 斜率优化 DP
- bzoj1010[HNOI2008]玩具装箱toy 斜率优化dp
- 一个菜鸟的独白
- 微信开发如何优雅的注入token
- 从零开始的指针运用1
- 【机房合作】类图如何导出代码框架
- ReentrantReadWriteLock读写锁
- [BZOJ1010][HNOI2008]玩具装箱(决策单调性/斜率优化DP)
- 带环链表 & 找出环中入口节点
- 职责分配的含义以及在软件开发中的重要作用(不良代码19行可以简化到3行)
- HTTP1.0、HTTP1.1 和 HTTP2.0 的区别
- 1301;计算a的立方
- hdu 1085 Holding Bin-Laden Captive!(母函数)
- 虚拟机下utuntu的找不到ip问题
- DDD领域驱动笔记
- Windows下安装mongodb,设置mongodb开机启动,随windows服务启动