_bzoj1010 [HNOI2008]玩具装箱toy【斜率优化dp】
2024-09-30 12:57:30
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1010
裸的斜率优化,第一次写队首维护的时候犯了个智障错误,队首维护就是维护队首,我怎么会那队尾两个点的斜率来进行比较。。。
保存斜率优化dp的模版。
#include <cstdio> const int maxn = 50005; int n, L, head_, tail;
long long s[maxn], f[maxn];
struct point {
long long x, y;
int id;
} que[maxn], t; inline long long poww(long long aa) {
return aa * aa;
}
inline long long g(int i) {
return s[i] + i - L - 1;
}
inline long long h(int i) {
return s[i] + i;
}
inline double getk(point & aa, point & ss) {
return (double)(ss.y - aa.y) / (double)(ss.x - aa.x);
} int main(void) {
scanf("%d%d", &n, &L);
for (int i = 1; i <= n; ++i) {
scanf("%lld", s + i);
s[i] += s[i - 1];
}
que[tail++] = (point){0, 0, 0};
long long tem;
int j;
for (int i = 1; i <= n; ++i) {
tem = g(i) << 1;
while (tail - head_ > 1 && getk(que[head_ + 1], que[head_]) <= (double)tem) {
++head_;
}
j = que[head_].id;
f[i] = f[j] + poww(i - j - 1 + s[i] - s[j] - L);
t = (point){h(i), f[i] + poww(h(i)), i};
while (tail - head_ > 1 && getk(t, que[tail - 1]) <= getk(que[tail - 1], que[tail - 2])) {
--tail;
}
que[tail++] = t;
}
printf("%lld\n", f[n]);
return 0;
}
最新文章
- [转]How do I use variables in Oracle SQL Developer?
- jq常用的方法
- maximo功能修改(初步理解)
- TinyFox在VS2015上的调试器
- I.MX6 SHT20 Linux 驱动移植
- [Java] 两种发起POST请求方法,并接收返回的响应内容的处理方式
- SQL Server 之 解锁
- 正则表达式从右往左进行匹配(Regex)
- 蒋金楠How ASP.NET MVC Works?[持续更新中…]
- javascript获取标签样式(获取背景为例)
- Extjs 兼容IE10
- C++11标准后的C++阅读书目
- js 的数学处理方法
- 初学spring笔记
- iOS上手指点击波纹效果的实现
- 【leetcode】443. String Compression
- 单页面登录——编码传参(oa会对#号会进行截断)
- NoSQL&;Redis
- echart四川地图
- Qt5.3.2_CentOS6.4_基本编程环境__20160306【勿删,繁琐】
热门文章
- Windows平台下Git(gitblit)服务器搭建
- vue + vue-lazyload 实现图片懒加载
- Malformed or corrupted AST file: &;#39;Unable to load module &;quot;...
- Oracle 模糊查询方法
- (八)unity4.6Ugui中文教程文档-------概要-UGUI Rich Text
- R.layout引用不了布局文件
- Responsive Nav
- myqsl02
- YTU 2892: I--免费看电影
- Java中去除字符串中的所有空格