# Hdu 3507 Print Article【斜率优化Dp入门】

351 篇文章 2 订阅
43 篇文章 0 订阅

## Print Article

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

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.

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

①设定Dp【i】表示到位子i的最小总价值和。那么有:

Dp【i】=min（Dp【i】，Dp【j】+(Sum【j】-Ssum【i】)^2 +m）；

②我们假设有：k<j，并且对于当前到点i的情况，选取j比k更优的话，那么有：

Dp【j】+(Sum【j】-Ssum【i】)^2 +m<=Dp【k】+(Sum【k】-Ssum【i】)^2 +m

（Dp【j】+sum【j】^2）-（Dp【k】+sum【k】^2）/2*（sum【j】-sum【k】）<=sum【i】；

（Yj-Yk）/（Xj-Xk）<=sum【i】；

③那么根据上述，我们可以有两个推论：

1.G【j，k】<=sum【i】，那么位子k就可以被淘汰。

2.G【j，k】<=G【i，j】并且有G【j，k】<=sum【i】，那么表示j比k更优，并且i比j更优，那么位子j是可以被淘汰的。

④我们知道，sum【i】是单调递增的，所以我们可以对位子维护一个单调队列来求解。

Ac代码：

#include<stdio.h>
#include<string.h>
using namespace std;
int que[650000];
int a[650000];
int sum[650000];
int dp[650000];
int n,m;
int A(int j,int k)
{
return (dp[j]+sum[j]*sum[j])-(dp[k]+sum[k]*sum[k]);
}
int B(int j,int k)
{
return 2*(sum[j]-sum[k]);
}
int Val(int i,int j)
{
return dp[j]+(sum[i]-sum[j])*(sum[i]-sum[j])+m;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
sum[0]=0;
memset(dp,0,sizeof(dp));
memset(que,0,sizeof(que));
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];
que[tot++]=0;
for(int i=1;i<=n;i++)
{
que[tot++]=i;
}
printf("%d\n",dp[n]);
}
}


• 1
点赞
• 1
收藏
觉得还不错? 一键收藏
• 0
评论
05-13 498
08-02 166
01-02 161
08-23 80
11-28 629
07-11 157
12-26 218
02-07
03-31
01-16 2109
04-27 2063
05-09 1440
04-05 1238

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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