hdu4258 斜率优化dp
2015-10-23 08:53
246 查看
参考下面文章 http://blog.csdn.net/balloons2012/article/details/7912296 感觉最重要的是 和当前位置有关的斜率,随下表递增单调。。这样凸包才可以求得最优解
</pre><pre name="code" class="cpp">#include <iostream> #include <cmath> #include <cstdio> #define N 1000010 #define LL long long using namespace std; struct point{ LL x, y; point(LL a=0, LL b = 0): x(a), y(b) { } void set(LL a, LL b) { x = a; y = b; } }; point que ; LL a ; LL dp ; bool mul(const point &a, const point &b, const point &c) { return (b.x-a.x) * (c.y-a.y) <= (b.y-a.y)*(c.x-a.x); } int main(int argc, char* argv[]) { LL n, c; while(scanf("%lld%lld", &n, &c) && (n && c)) { for(LL i=0; i<n; ++i) { scanf("%lld", &a[i]); } int head = 0, tail = 0; que[tail++].set(a[0], a[0]*a[0]); dp[0] = c; for(LL i=1; i<n; ++i) { point pp(a[i], dp[i-1]+a[i]*a[i]); while(head+1 < tail && mul(que[tail-2], que[tail-1], pp)) --tail; que[tail++] = pp; while(head+1 < tail && que[head].y-2*a[i]*que[head].x >= que[head+1].y-2*a[i]*que[head+1].x) ++head; dp[i] = que[head].y - 2*a[i]*que[head].x + a[i]*a[i] + c; } printf("%lld\n", dp[n-1]); } return 0; }
相关文章推荐
- HDOJ 2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)
- 第七周项目6--停车场
- 九度OJ 1094:String Matching(字符串匹配) (计数)
- 九度OJ 1094:String Matching(字符串匹配) (计数)
- 第8周项目1——建立顺序串的算法库
- 排队看病模拟
- Android ExpandableListView开发简介
- Objective-C 类,对象,属性,方法
- java正则表达式学习
- swift中关于表情键盘,抖动问题处理
- 2015-10-23 【项目4-字符串加密】
- 第9周 项目3—稀疏矩阵的三元组表示的实现及应用
- 用switch语句实现多分支结构
- 九度OJ 1093:WERTYU (翻译)
- Windows 下的进程间通讯及数据共享 - 云风大神这篇文章05年写的,已经那么透彻
- 在SDcard中创建文件与读取文件
- 九度OJ 1093:WERTYU (翻译)
- Android Studio 设置
- C盘使用内存突然间增加好多
- CV codes代码分类整理合集