传送门: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;
}

  

最新文章

  1. [转]How do I use variables in Oracle SQL Developer?
  2. jq常用的方法
  3. maximo功能修改(初步理解)
  4. TinyFox在VS2015上的调试器
  5. I.MX6 SHT20 Linux 驱动移植
  6. [Java] 两种发起POST请求方法,并接收返回的响应内容的处理方式
  7. SQL Server 之 解锁
  8. 正则表达式从右往左进行匹配(Regex)
  9. 蒋金楠How ASP.NET MVC Works?[持续更新中…]
  10. javascript获取标签样式(获取背景为例)
  11. Extjs 兼容IE10
  12. C++11标准后的C++阅读书目
  13. js 的数学处理方法
  14. 初学spring笔记
  15. iOS上手指点击波纹效果的实现
  16. 【leetcode】443. String Compression
  17. 单页面登录——编码传参(oa会对#号会进行截断)
  18. NoSQL&amp;Redis
  19. echart四川地图
  20. Qt5.3.2_CentOS6.4_基本编程环境__20160306【勿删,繁琐】

热门文章

  1. Windows平台下Git(gitblit)服务器搭建
  2. vue + vue-lazyload 实现图片懒加载
  3. Malformed or corrupted AST file: &amp;#39;Unable to load module &amp;quot;...
  4. Oracle 模糊查询方法
  5. (八)unity4.6Ugui中文教程文档-------概要-UGUI Rich Text
  6. R.layout引用不了布局文件
  7. Responsive Nav
  8. myqsl02
  9. YTU 2892: I--免费看电影
  10. Java中去除字符串中的所有空格