hdu 3507 斜率优化DP入门题
来源:互联网 发布:模拟城市mac版 编辑:程序博客网 时间:2024/06/11 23:41
http://acm.hdu.edu.cn/showproblem.php?pid=3507
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
/**hdu3507 斜率优化dp 膜拜大神:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html*/#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int N=500005;int dp[N];int q[N];int sum[N];int head,tail,n,m;int get_dp(int i,int j){ 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){ return 2*sum[j]-2*sum[k];}int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=1; i<=n; i++) scanf("%d",&sum[i]); sum[0]=dp[0]=0; for(int i=1; i<=n; i++) sum[i]+=sum[i-1]; head=tail=0; q[tail++]=0; for(int i=1; i<=n; i++) { while(head+1<tail&&get_up(q[head+1],q[head])<=sum[i]*get_down(q[head+1],q[head])) head++; dp[i]=get_dp(i,q[head]); while(head+1<tail&&get_up(i,q[tail-1])*get_down(q[tail-1],q[tail-2])<=get_up(q[tail-1],q[tail-2])*get_down(i,q[tail-1])) tail--; q[tail++]=i; } printf("%d\n",dp[n]); } return 0;}
0 0
- hdu 3507 斜率优化DP入门题
- hdu 3507 斜率优化dp 入门学习
- hdu 3507 斜率优化dp入门
- hdu 3507 斜率优化dp
- hdu 3507 斜率优化DP
- hdu 3507 斜率优化dp
- hdu 3507 斜率dp优化
- hdu 3507 斜率优化dp
- HDU 3507 Print Article DP(斜率优化入门)
- Hdu 3507 Print Article【斜率优化Dp入门】
- hdu 2993 MAX Average Problem(DP+斜率优化入门题)
- hdu 2993 斜率优化 dp 入门学习
- 斜率dp优化两题(poj 3709 hdu 3507)
- hdu 3507 Print Article 斜率优化dp
- HDU 3507 Print Article【斜率优化dp】
- hdu 3507 Print Article(DP+斜率优化)
- hdu 3507 Print Article 斜率优化DP
- HDU 3507 Print Article 斜率优化DP
- Zend Framework 2 入门实例-Sql Service(MSSQL)数据库链接
- vim ctrl+s就卡死
- Linux查看内存
- 经典的Android面试题
- 请关闭你的等死模式
- hdu 3507 斜率优化DP入门题
- ReactiveCocoa Weak-Strong Dance
- Java将域名转换成IP
- hdu - 3498 - whosyourdaddy(重复覆盖DLX)
- 2014-10-27Android学习------SQLite数据库操作(二)-----数据库的创建--SQLiteHelper extends SQLiteOpenHelper
- Zend Framework 2 入门实例-Mysql数据库链接
- ExtJS表格的操作:取选择项
- oracle查看和修改最大连接数
- 早安心语:微笑有时候说明你很坚强