HDU3507:Print Article(斜率优化dp)

传送门

题意:
现有\(n\)个数,每个数的值为\(a_i\),现在可以把数划分为多段,每一段的代价为\((\sum_{k=i}^{j}c_i)^2+M\)。
问怎么划分,代价最小。

思路:
考虑dp,那么dp式子很简单:
\[
dp(i)=min\{dp(j)+(S_i-S_j)^2+M\}
\]
注意这种\(dp\)形式,后面加上的部分与\(i,j\)两个变量有关,这种一般可以考虑分离变量然后斜率dp优化。
(PS.如果可以分为多个部分,每个部分只和一个有关,那么可以考虑单调队列优化。from进阶指南)
那么就尝试写成直线形式:
\[
dp_j+S_j^2=2S_iS_j+dp_i-S_i^2-M
\]

这里我们以\((S_j,dp_j+S_j^2)\)为一个点,然后用队列维护一个下凸壳就行了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 500005, MOD = 1e9 + 7;
int n, m;
ll sum[N], dp[N];
int a[N], q[N];
int main() {
#ifdef heyuhhh
    freopen("input.in", "r", stdin);
#else
    ios::sync_with_stdio(false); cin.tie(0);
#endif
    while(cin >> n >> m) {
        for(int i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] + a[i];
        dp[0] = 0;
        int l = 1, r = 1;
        q[1] = 0;
        auto f = [&](int i) {
            return dp[i] + sum[i] * sum[i];
        };
        for(int i = 1; i <= n; i++) {
            while(l < r && f(q[l + 1]) - f(q[l]) <= 2 * sum[i] * (sum[q[l + 1]] - sum[q[l]])) ++l;
            dp[i] = dp[q[l]] + (sum[i] - sum[q[l]]) * (sum[i] - sum[q[l]]) + m;
            while(l < r && (f(i) - f(q[r])) * (sum[q[r]] - sum[q[r - 1]]) <= (f(q[r]) - f(q[r - 1])) * (sum[i] - sum[q[r]])) --r;
            q[++r] = i;
        }
        cout << dp[n] << '\n';
    }
    return 0;
}

原文地址:https://www.cnblogs.com/heyuhhh/p/11415306.html

时间: 2024-10-21 13:11:02

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

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

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

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

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