【Hdu3507】Print Article(斜率优化)

Description

题意:给N个数,按顺序全部取走,每次取一段连续的区间,代价为\((S[i]-S[j])^2+M\)

其中M为一个给定的常数,\(S[i]\)为前缀和

\(N\leq 500000\)

Solution

常规的方程:\(dp[i]=min\{dp[j]+(S[i]-S[j])^2+M\}, j<i\)

\(O(n^2)\)是过不了50万的,用斜率优化

设有\(k<j<i\) 使得决策j更优,那么有

\(dp[j]+(S[i]-S[j])^2+M<dp[k]+(S[i]-S[k])^2+M\)

整理得 \(\frac{(dp[j]+S[j]^2)-(dp[k]+S[k]^2)}{(S[j]-S[k])}<2*S[i]\)

令\(f[i]=dp[i]+S[i]^2\),所以有\(\frac{f[j]-f[k]}{s[j]-s[k]}<2*S[i]\)

此时决策j更优,反之决策k优

易证\(f[i]\)也单调递增,所以可用单调队列维护

Code

#include <cstdio>
#include <algorithm>
#define N 500010
#define squ(x) ((x)*(x))
using namespace std;

int n,M,l,r,q[N],s[N],dp[N];

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

inline int f(int k,int j){return dp[j]+squ(s[j])-(dp[k]+squ(s[k]));}
inline int g(int k,int j){return s[j]-s[k];}

void DP(){
    l=1,r=0;q[++r]=0;dp[0]=0;
    for(int i=1;i<=n;++i){
        while(l<r&&f(q[l],q[l+1])<=2*s[i]*g(q[l],q[l+1])) l++;
        int j=q[l];
        dp[i]=dp[j]+squ(s[i]-s[j])+M;
        while(l<r&&f(q[r],i)*g(q[r-1],q[r])<f(q[r-1],q[r])*g(q[r],i)) r--;//保证斜率单调
        q[++r]=i;
    }
}

int main(){
    while(~scanf("%d%d",&n,&M)){
        for(int i=1;i<=n;++i) s[i]=s[i-1]+read();
        DP();
        printf("%d\n",dp[n]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/void-f/p/8404311.html

时间: 2024-10-19 05:33:38

【Hdu3507】Print Article(斜率优化)的相关文章

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

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

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

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>

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

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

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

HDU3507 Print Article

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) 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 to