HDOJ-3507 Print Article (斜率优化dp)

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

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

题意:
输出N个数字,可以连续的输出一串数字,连续输出一串时费用是 “这串数字和的平方加上一个常数M”
题解:
设dp[i]表示输出到i的时候最少的花费,sum[i]表示从a[1]到a[i]的数字和。方程就是:
dp[i]=dp[j]+M+(sum[i]-sum[j])^2;

假设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]

此时设k < j < i,如果g[i,j] < g[j,k],那么j点便永远不可能成为最优解,可以直接将它踢出我们的最优解集。

1.我们假设g[i,j] < sum[i],那么就是说i点要比j点优,排除j点。

2.如果g[i,j] > = sum[i],那么j点此时是比i点要更优,但是同时g[j,k] > g[i,j] > sum[i]。这说明还有k点会比j点更优,同样排除j点。

对于这题我们对于斜率优化做法可以总结如下:

1,用一个单调队列来维护解集。

2,假设队列中从头到尾已经有元素a b c。那么当d要入队的时候,我们维护队列的上凸性质,即如果g[d,c]

#include<bits\stdc++.h>
using namespace std;
const int N = 5 * 1e5 + 5;
int dp[N], sum[N], num[N], q[N], M, n;

int getDP(int i, int j){
    return dp[j] + M + (sum[i] - sum[j])*(sum[i] - sum[j]);
}

int getUP(int i, int j){
    return dp[i] + sum[i] * sum[i] - (dp[j] + sum[j] * sum[j]);
}

int getDOWN(int i, int j){
    return 2 * (sum[i] - sum[j]);
}

int main() {
    while (scanf("%d%d", &n, &M) == 2) {

        int front = 0, rear = 1;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &sum[i]);
        }
        sum[0] = dp[0] = 0;
        for (int i = 1; i <= n; i++) {
            sum[i] += sum[i - 1];
        }
        q[0] = 0;
        for (int i = 1; i <= n; i++) {
            //front+1<rear时说明,队列中的元素个数包括i大于等于三个
            //先用新加入的i,判断队列开头是否有满足g[b,a]<sum[i]的点,如果有就把a扔掉,也就是front++,令其出栈
            //g[b,a]<sum[i]即为getUP(b,a)< sum[i] * getDOWN(b,a)
            while (front + 1 < rear&&getUP(q[front + 1], q[front]) <= getDOWN(q[front + 1], q[front])*sum[i]) front++;
            dp[i] = getDP(i, q[front]);
            //i入队时,判断g[i, rear -1]<g[rear -1, rear - 2],如果满足就删去rear - 1,即让rear--,用i覆盖rear - 1
            while (front + 1 < rear&&getUP(i, q[rear - 1])*getDOWN(q[rear - 1], q[rear - 2]) <= getUP(q[rear - 1], q[rear - 2])*getDOWN(i, q[rear - 1])) rear--;
            q[rear++] = i;
        }
        printf("%d\n", dp[n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值