HDU3507_Print Article

本文介绍了一种通过动态规划和斜率优化解决最小成本打印文章问题的方法,目标是最小化打印文章时的总成本,考虑了每行打印字数的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


代码:

#include <stdio.h>
const int maxn = 500010;
int dp[maxn];
int que[maxn];
int sum[maxn];
int head, tail, n, m;
int Get_dp(int i, int j){ //yj-yk的部分
    return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
}
int Get_up(int j, int k){
    return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);
}
int Get_down(int j, int k){ //xj-xk的部分
    return (sum[j]-sum[k])<<1;
}
int solve(){
    int i;
    head = 0, tail = -1;
    que[++tail] = 0;
    for(i = 1; i <= n; i++){
        while(head < tail && Get_up(que[head+1], que[head]) <= sum[i]*Get_down(que[head+1], que[head])) ++head;
        dp[i] = Get_dp(i, que[head]);
        while(head < tail && Get_up(i, que[tail])*Get_down(que[tail], que[tail-1]) <= Get_up(que[tail], que[tail-1])*Get_down(i, que[tail])) --tail;
        que[++tail] = i;
    }
    return dp[n];
}
int main()
{
    dp[0] = sum[0] = 0;
    while(scanf("%d %d", &n, &m)==2){
        int i;
        for(i = 1; i <= n; i++){
            scanf("%d", &sum[i]);
            sum[i] += sum[i-1];
        }
        printf("%d\n", solve());
    }
    return 0;
}


题意:有N个数,现要将它们分成连续的若干段,每段的代价为(∑Ci)^2+M,求最小的代价。

解题思路:用dp[i]表示到第i个为止划分出来的最小值,可以很容易想到转移方程:

                 dp[i]=min(dp[j]+(sum[i]-sum[j])^2+m)(1<=j<i) 二维方程再看数据范围肯定超时。

对DP做斜率优化 :

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])。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值