hdu 3075 Print Article

本文解析了一道经典的斜率DP题目,通过优化状态转移方程dp[i]=dp[j]+M+(sum[i]-sum[j])^2来求解最小打印成本。文章详细介绍了如何维护一个特殊的队列进行斜率优化,确保复杂度可控。

题目描述:

Print Article

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


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
 

Recommend
zhengfeng

第一道经典斜率dp!!!!

思路;
首先,这是一道dp ,很容易写出状态转移方程:
dp[i]=dp[j]+M+(sum[i]-sum[j])^2;

假设 有 k<j<i 且j决策更优 得到:
       dp[j]+(sum[i]-sum[j])^2<dp[k]+(sum[i]-sum[k])^2
化简:
       ((dp[j]+sum[j]^2-(dp[k]+sum[k]^2))<sum[i]*(sum[j]-sum[k])
设 y[i]=dp[i]+sum[i]^2 ,x[i]=sum[i]
上式等价于    (y[j]-y[k])/(x[j]-x[k])<sum[i];


用g(j,k) 表示上式左端 ;

下面证明 当k<j<i 时若g(j,k) >= g(i,j)那么j不能是最优解

1) : 若g(i,j) < sum[ii]  ,那么i比j优

2) : 若g(i,j)>=sum[ii],则g(j,k)>g(i,j)>sum[ii] k比j优


由上我们可以维护这么一个队列 ( 有人称之为凸性) 在队列中 若有k<j<i 则必有g(j,k)<g(i,j)

每次更新的时候,从队头开始查找,直到有其(y[he+1]-y(he])/(x[he+1]-x[he])<sum[i]) 为之 。


以上优化就称之斜率优化:

附上小代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define inf 0x7777777
#define int long long

using namespace std;
int const nMax = 500010;

int a[nMax];
int dp[nMax];
int sum[nMax];
int n,M;

int q[nMax],he,ta;
int dy(int i,int j){
    return dp[i]+sum[i]*sum[i]-dp[j]-sum[j]*sum[j];
}
int dx(int i,int j){
    return 2*(sum[i]-sum[j]);
}
void query(int i){
    while(he<ta){
        if(dy(q[he+1],q[he])<=sum[i]*(dx(q[he+1],q[he])))he++;
        else break;
    }
    dp[i]=dp[q[he]]+(sum[i]-sum[q[he]])*(sum[i]-sum[q[he]])+M;
    return ;
}
void insert(int i){
    while(he<ta){
        int y2=q[ta],y1=q[ta-1];
        if(dy(y2,y1)*(dx(i,y2))>=dy(i,y2)*dx(y2,y1))ta--;
        else break;
    }
    q[++ta]=i;
    return ;
}

main()
{
    while(~scanf("%I64d%I64d",&n,&M)){
        sum[0]=0;
        for(int i=1;i<=n;i++){
            scanf("%I64d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        dp[0]=0;
        ta=he=0;
        q[0]=0;
        int ans=inf;
        for(int i=1;i<=n;i++){
            query(i);
            ans=min(ans,dp[i]);
            insert(i);
        }
        printf("%I64d\n",dp[n]);
    }
    return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值