向前走莫回头❤

【HDU 3507】Print Article(斜率优化dp)

Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 8583 Accepted Submission(s): 2673

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

Source
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT

【题解】【斜率优化dp】
首先根据题意,设dp[i]表示输出到i的时候最少的花费,sum[i]表示从a[1]到a[i]的数字和,则转移方程为:**f[i]=f[j]+m+(sum[i]sum[j])2
然而,500000,woc,明显会超时啊!
加优化吧——斜率优化(窝也是第一次写哈)

假设k<j<i。如果在j的时候决策要比在k的时候决策好,那么也是就是dp[j]+M+(sum[i]sum[j])2<dp[k]+M+(sum[i]sum[k])2。(因为是最小花费嘛,所以较优就是小于)
两边移项一下,得到:(dp[j]+num[j]2-(dp[k]+num[k]2))/(2*num[j]-num[k]))<sum[i]。我们把dp[j]-num[j]2看做是yj,把2*num[j]看成是xj。
那么不就是yj-yk/xj-xk<sum[i]么? 左边是不是斜率的表示?
那么yj-yk/xj-xk<sum[i]说明了什么呢? 我们前面是不是假设j的决策比k的决策要好才得到这个表示的? 如果是的话,那么就说明g[j,k]=yj-jk/xj-xk<sum[i]代表这j的决策比k的决策要更优。

关键的来了:现在从左到右,还是设k<j<i,如果g[i,j]<g[j,k],那么j点便永远不可能成为最优解,可以直接将它踢出我们的最优解集。为什么呢?
我们假设g[i,j]<sum[i],那么就是说i点要比j点优,排除j点。
如果g[i,j]>=sum[i],那么j点此时是比i点要更优,但是同时g[j,k]>g[i,j]>sum[i]。这说明还有k点会比j点更优,同样排除j点。
排除多余的点,这便是一种优化!

接下来看看如何找最优解。
设k<j<i。由于我们排除了g[i,j]<g[j,k]的情况,所以整个有效点集呈现一种上凸性质,即k j的斜率要大于j i的斜率。
这里写图片描述
这样,从左到右,斜率之间就是单调递减的了。当我们的最优解取得在j点的时候,那么k点不可能再取得比j点更优的解了,于是k点也可以排除。换句话说,j点之前的点全部不可能再比j点更优了,可以全部从解集中排除。

于是对于这题我们对于斜率优化做法可以总结如下:
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])。
(自:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int sum[500010],f[500010],q[500010],h,t;

int getdp(int i,int j)
{
    return f[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
}
int getup(int j,int k)//yj-yk的部分
{
    return f[j]+sum[j]*sum[j]-(f[k]+sum[k]*sum[k]);
}
int getdown(int j,int k)
{
    return 2*(sum[j]-sum[k]);
}
int main()
{
    int i,j;
    while((scanf("%d%d",&n,&m)==2))
     {
        for(i=1;i<=n;++i) scanf("%d",&sum[i]);
        sum[0]=f[0]=0;
        for(i=1;i<=n;++i) sum[i]+=sum[i-1];
        h=t=0; q[t++]=0;
        for(i=1;i<=n;++i)
         {
            while(h+1<t && getup(q[h+1],q[h])<=sum[i]*getdown(q[h+1],q[h])) h++;
            f[i]=getdp(i,q[h]);
            while(h+1<t && getup(i,q[t-1])*getdown(q[t-1],q[t-2])<=getup(q[t-1],q[t-2])*getdown(i,q[t-1])) t--;
            q[t++]=i;
          }
        printf("%d\n",f[n]);
     }
    return 0;    
 } 
posted @ 2016-04-24 17:27  lris0-0  阅读(87)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m