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

来源:互联网 发布:淘宝ka店 编辑:程序博客网 时间:2024/06/17 04:24

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 559575
 

Sample Output
230
 

Author
Xnozero

题目大意:

给出N个数,我们可以将其分成若干个连续的子段,每个子段的贡献值为m+子段和的平方。问最小总价值和。


思路:


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

Dp【i】=min(Dp【i】,Dp【j】+(Sum【j】-Ssum【i】)^2 +m);

很显然,我们不能去暴力Dp,这里要用到斜率优化。


②我们假设有: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==(Dp【j】+sum【j】^2) Xj=2*sum【j】的话,能够化简不等式为:

(Yj-Yk)/(Xj-Xk)<=sum【i】;

我们令G【j,k】=(Yj-Yk)/(Xj-Xk);我们知道若存在G【j,k】<=sum【i】,那么位子j的选择一定优于位子k的选择。

所以我们可以将位子k淘汰掉。


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

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];        int head=0,tot=0;        que[tot++]=0;        for(int i=1;i<=n;i++)        {            while(head+1<tot&&A(que[head+1],que[head])<=sum[i]*B(que[head+1],que[head]))head++;            dp[i]=Val(i,que[head]);            while(head+1<tot&&A(i,que[tot-1])*B(que[tot-1],que[tot-2])<=A(que[tot-1],que[tot-2])*B(i,que[tot-1]))tot--;            que[tot++]=i;        }        printf("%d\n",dp[n]);    }}