HDU3507 Print Article

针对一篇文章,通过动态规划和斜率优化求解最小成本打印方案。考虑每行打印的单词数量及额外成本,通过DP算法寻找最优解。

 

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 9301    Accepted Submission(s): 2893


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.
 

题意概括:有N个数字需要输出,输出过程中可以随意换行。每行需要付出常数M的额外代价。若一行中连续输出(只输出一个也算),则需要的代价依照上述公式计算。

    求代价最小值。

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 5
5
9
5
7
5
 
Sample Output
230
 

 

Author
Xnozero
 

 

Source
 

 

Recommend
zhengfeng

 

很容易想到DP解决。

设f[i]表示输出前i个数字的花费,则有转移方程$f[i]=min(f[i],f[j]+(sum[i]-sum[j])^2+M),0<j<i$

n范围很大,按这个式子暴力动规会TLE,所以需要斜率优化。

若有k满足0<k<j<i  ,且用k算比用j优(算出的花费更小),则有 $f[k]+(sum[i]-sum[k])^2+M<=f[j]+(sum[i]-sum[j])^2+M))$

各种变形化简得到:$[(dp[j]+sum[j]*sum[j])-(dp[k]+sum[k]*sum[k])] / 2(sum[j]-sum[k]) <=sum[i]$

设:$ y=dp[j]+sum[j]*sum[j] $  $x=2*sum[j]$

可以得到斜率表达式:$(yj-yk)/(xj-xk) <= sum[i] $

据此维护一个斜率逐渐增大的队列来dp,即可快速出解

 

 1  /**/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 const int mxn=500020;
 9 int f[mxn];
10 int sum[mxn];
11 int q[mxn];
12 int n,m;
13 int up(int x,int y){//式子的分子 
14     return (f[x]+sum[x]*sum[x])-(f[y]+sum[y]*sum[y]);
15 }
16 int down(int x,int y){//式子的分母 
17     return 2*(sum[x]-sum[y]);
18 }
19 int dp(int x,int y){//dp值 
20     return f[y]+(sum[x]-sum[y])*(sum[x]-sum[y])+m;
21 }
22 int main(){
23     while(scanf("%d%d",&n,&m)!=EOF){
24         int i,j;
25         int hd=0,tl=0;
26         sum[0]=0;f[0]=0;
27         for(int x,i=1;i<=n;i++){
28             scanf("%d",&x);
29             sum[i]=sum[i-1]+x;
30         }
31         q[tl++]=0;
32         for(i=1;i<=n;i++){
33             while(hd+1<tl && up(q[hd+1],q[hd])<=sum[i]*down(q[hd+1],q[hd]))hd++;
34             f[i]=dp(i,q[hd]);
35             while(hd+1<tl && up(i,q[tl-1])*down(q[tl-1],q[tl-2])<=up(q[tl-1],q[tl-2])*down(i,q[tl-1]))tl--;
36             q[tl++]=i;
37         }
38         printf("%d\n",f[n]);
39     }
40     return 0;
41 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5704639.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值