HDU3507 Print Article (斜率优化DP基础复习)

传送门

大意:打印一篇文章,连续打印一堆字的花费是这一堆的和的平方加上一个常数M。

首先我们写出状态转移方程 :f[i]=f[j]+(sum[i]?sum[j])2+M;

设 j 优于 k.

那么有 f[j]+(sum[i]?sum[j])2<f[k]+(sum[i]?sum[k])2

移项得出

(f[j]+sum[j]2)?(f[k]+sum[j]2)2?(sum[j]+sum[k])<sum[i]

这就是一个很理想的斜率式了。

#include<cstdio>
#include<cctype>
const int MAXN = 5 * 1e6;
void GET(int &n)
{
    char c, f=1;n=0;
    do{c=getchar(); if(c==‘-‘)f=-1;}while(!isdigit(c));
    while(isdigit(c)){n = n *10 + c -‘0‘; c= getchar();}
    n = n * f;
}
int q[MAXN], f[MAXN], c[MAXN], s, t, n, m;
inline int Y(int j,int k)
{
    return f[j]+c[j]*c[j]-(f[k]+c[k]*c[k]);
}
inline int X(int j,int k)
{
    return 2*(c[j]-c[k]);
}
int main()
{
    while(~scanf("%d%d", &n,&m))
    {
        for(int i = 1; i <= n; i++)
        {
            GET(c[i]);
            c[i] += c[i-1];
        }
        s = 1, t = 0;
        q[++t] = 0;
        for(int i = 1; i <= n; i++)
        {
            while(s < t && Y(q[s+1], q[s]) <= c[i] * X(q[s+1], q[s])) ++ s;
            f[i] = f[q[s]] + m + (c[i] - c[q[s]])*(c[i] - c[q[s]]);
            while(s < t && X(q[t-1], q[t]) * Y(q[t], i) <= X(q[t], i) * Y(q[t-1], q[t])) -- t;
            q[++t] = i;
        }
        printf("%d\n", f[n]);
    }
    return 0;
}

版权声明:请随意转载O(∩_∩)O

时间: 2024-10-13 03:08:15

HDU3507 Print Article (斜率优化DP基础复习)的相关文章

hdu3507 Print Article[斜率优化dp入门题]

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 11761    Accepted Submission(s): 3586 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antiqu

[hdu3507 Print Article]斜率优化dp入门

题意:需要打印n个正整数,1个数要么单独打印要么和前面一个数一起打印,1次打印1组数的代价为这组数的和的平方加上常数M.求最小代价. 思路:如果令dp[i]为打印前i个数的最小代价,那么有 dp[i]=min(dp[j]+(sum[i]-sum[j])2+M),j<i 直接枚举转移是O(n2)的,然而这个方程可以利用斜率优化将复杂度降到O(n). 根据斜率优化的一般思路,对当前考虑的状态i,考虑决策j和k(j<k),如果k比j优,那么根据转移方程有:dp[k]+(sum[i]-sum[k])2

Print Article /// 斜率优化DP oj26302

题目大意: 经典题 数学分析 G(a,b)<sum[i]时 a优于b G(a,b)<G(b,c)<sum[i]时 b必不为最优 #include <bits/stdc++.h> #define N 500005 using namespace std; int n,m,dp[N],deq[N],sum[N]; // deq[]为单调队列 sum[]为数组的前缀和 int DP(int i,int j) { return dp[j]+m+(sum[i]-sum[j])*(sum

hdu 3507 Print Article —— 斜率优化DP

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3507 设 f[i],则 f[i] = f[j] + (s[i]-s[j])*(s[i]-s[j]) + m 即 f[j] + s[j]*s[j] = 2*s[i]*s[j] + f[i] - s[i]*s[i] - m 于是维护下凸包即可: 写成 double 的 slp 总是不对,把分母乘到对面就对了... 代码如下: #include<iostream> #include<cstdio>

HDU 3507 Print Article 斜率优化

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4810    Accepted Submission(s): 1451 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

hdu3507Print Article(斜率优化dp)

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12824    Accepted Submission(s): 3967 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antiqu

Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4990    Accepted Submission(s): 1509 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

HDU3507 Print Article(经典斜率优化dp)

一道很老的斜率优化dp 斜率优化看上去很难,其实是有技巧的 . 对于dp题目,如果你想优化他,一定要先列出朴素的表达式并观察性质 对于本题我们可以发现,如果要更新dp[i],我们就要从前面找到dp[j]+(s[i]-s[j])^2+m的最小值,其中s是前缀和 我们就可以猜测,一定有很多不可能转移的内容,我们应该如何删除它从而降低复杂度. 那么我们假设k<j,当i出现之后,k就不可能作为答案,那么这些k在i处满足的性质就是 dp[j]+(s[i]-s[j])^2+m<=dp[k]+(s[i]-s

hdu3507 Print Article(斜率优化入门)(pascal)

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 t