HDU3507_Print Article
来源:互联网 发布:cv 怎么写 知乎 编辑:程序博客网 时间:2024/05/29 15:37
Problem Description
Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost
M is a const number.
Now Zero want to know the minimum cost in order to arrange the article perfectly.
One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost
M is a const number.
Now Zero want to know the minimum cost in order to arrange the article perfectly.
Input
There are many test cases. For each test case, There are two numbers N and M in the first line (0 ≤ n ≤500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2 to N + 1 lines. Input are terminated by EOF.
Output
A single number, meaning the mininum cost to print the article.
Sample Input
5 559575
Sample Output
230
代码:
#include <stdio.h>const int maxn = 500010;int dp[maxn];int que[maxn];int sum[maxn];int head, tail, n, m;int Get_dp(int i, int j){ //yj-yk的部分 return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);}int Get_up(int j, int k){ return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);}int Get_down(int j, int k){ //xj-xk的部分 return (sum[j]-sum[k])<<1;}int solve(){ int i; head = 0, tail = -1; que[++tail] = 0; for(i = 1; i <= n; i++){ while(head < tail && Get_up(que[head+1], que[head]) <= sum[i]*Get_down(que[head+1], que[head])) ++head; dp[i] = Get_dp(i, que[head]); while(head < tail && Get_up(i, que[tail])*Get_down(que[tail], que[tail-1]) <= Get_up(que[tail], que[tail-1])*Get_down(i, que[tail])) --tail; que[++tail] = i; } return dp[n];}int main(){ dp[0] = sum[0] = 0; while(scanf("%d %d", &n, &m)==2){ int i; for(i = 1; i <= n; i++){ scanf("%d", &sum[i]); sum[i] += sum[i-1]; } printf("%d\n", solve()); } return 0;}
题意:有N个数,现要将它们分成连续的若干段,每段的代价为(∑Ci)^2+M,求最小的代价。
解题思路:用dp[i]表示到第i个为止划分出来的最小值,可以很容易想到转移方程:
dp[i]=min(dp[j]+(sum[i]-sum[j])^2+m)(1<=j<i) 二维方程再看数据范围肯定超时。
对DP做斜率优化 :
1,用一个单调队列来维护解集。
2,假设队列中从头到尾已经有元素a b c。那么当d要入队的时候,我们维护队列的上凸性质,即如果g[d,c]<g[c,b],那么就将c点删除。直到找到g[d,x]>=g[x,y]为止,并将d点加入在该位置中。
3,求解时候,从队头开始,如果已有元素a b c,当i点要求解时,如果g[b,a]<sum[i],那么说明b点比a点更优,a点可以排除,于是a出队。最后dp[i]=getDp(q[head])。
0 0
- HDU3507_Print Article
- article
- article
- Article
- article(网络)
- first article
- first article
- First Article
- new article
- first article
- test article
- BugFree article
- first article
- test article
- article collections
- First Article
- First Article
- First article
- kerveros认证过程
- 新的征程-----致幡然醒悟的自己
- 5月17日安卓开发环境的安装和打电话的安卓应用
- STM32推挽输出、开漏输出
- winform清空DataGridView中的数据
- HDU3507_Print Article
- Cocos2d-x中父节点scale对子节点的影响
- ExecutorService产生的Thread命名
- Dalvik VM 和 JVM 的比较
- Java虚拟机和android虚拟机之间的区别
- 页面置顶的悬浮按钮
- Skin++ For VS2010 使用教程
- OSCache学习(1)Cache使用的必要以及OSCache的简介
- 安卓第一天