[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+M ≤ dp[j]+(sum[i]-sum[j])2+M

整理可得:dp[k]+sum[k]2-2*sum[i]*sum[k] ≤ dp[j]+sum[j]2-2*sum[i]*sum[j]

然后进一步得到:[(dp[k]+sum[k]2)-(dp[j]+sum[j]2)] / (2*sum[k] - 2*sum[j]) ≤ sum[i]

如果令 y(i)=dp[i]+sum[i]2,x(i)=2*sum[i],那么有:( y(k)-y(j) ) / ( x(k)-x(j) ) ≤ sum[i],不妨令左边=G[j][k],即"j到k的斜率",G[j][k] ≤ sum[i]

注意,上面的推理的因果是等价的,也就是说 "k比j优" ↔ "( y(k)-y(j) ) / ( x(k)-x(j) ) ≤ sum[i]成立"

如果从小到大计算每个状态,那么(1)在某次计算状态i时,k比j优,由于sum数组单调递增,所以在以后的状态计算里面k都比j优(2)考虑三个状态i,j,k(i<j<k),满足G[i][j]≥G[j][k],那么在计算状态t(>k)的时候,{ 假设G[j][k]≤sum[t],k就比j优,否则G[j][k]>sum[t],那么显然有G[i][j]>sum[t],所以j不比i优 },所以对于t>k而言,j既没有k优也没有i优,完全可以舍弃。

在(2)的约束下,所有可能成为子状态的点构成了1个凸包,假设当前在计算状态i,这个凸包中最“前面”的两个点依次为j,k,如果G[j][k]≤sum[i],那么k比j优,把i从凸包里面删掉然后继续这样考虑,否则有G[j][k]>sum[i],说明j是最优的,因为对任意t∈(k,i)&&t∈凸包,都有G[j][t]>G[j][k]>sum[i],也就是没有比j更优的了。

虽然推理过程比较多,但是最后的结论非常的优美,程序也非常短,更重要的是,直接将原来O(n2)的复杂度降成了线性!没有比这更激动人心的了

#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define mset(a, x) memset(a, x, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(b))
#define cas() int T, cas = 0; cin >> T; while (T --)
template<typename T>bool umax(T&a, const T&b){return a<b?(a=b,true):false;}
template<typename T>bool umin(T&a, const T&b){return b<a?(a=b,true):false;}
typedef long long ll;
typedef pair<int, int> pii;
#ifndef ONLINE_JUDGE
    #include "local.h"
#endif
const int N = 5e5 + 7;
int head, tail;
pii q[N];
int n, m, x, sum[N];

int sqr(int x) { return x * x;}
int getY(int p) { return q[p].second + sqr(sum[q[p].first]); }
int getX(int p) { return 2 * sum[q[p].first]; }
int up(int p) { return getY(p + 1) - getY(p); }
int down(int p) { return getX(p + 1) - getX(p); }
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    while (cin >> n >> m) {
        for (int i = 1; i <= n; i ++) {
            scanf("%d", &x);
            sum[i] = sum[i - 1] + x;
        }
        head = tail = 0;
        q[tail ++] = mp(0, 0);
        for (int i = 1; i <= n; i ++) {
            while (tail - head > 1 && up(head) <= down(head) * sum[i]) head ++;
            q[tail ++] = mp(i, q[head].second + sqr(sum[i] - sum[q[head].first]) + m);
            while (tail - head > 2 && up(tail - 3) * down(tail - 2) >= up(tail - 2) * down(tail - 3)) {
                swap(q[tail - 2], q[tail - 1]);
                tail --;
            }
        }
        cout << q[tail - 1].second << endl;
    }
    return 0;
}

  

时间: 2024-12-17 20:01:40

[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基础复习)

传送门 大意:打印一篇文章,连续打印一堆字的花费是这一堆的和的平方加上一个常数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>

hdu3507之斜率优化DP入门

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

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

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as

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

APIO2010 特别行动队 &amp; 斜率优化DP入门讲解

做完此题之后 自己应该算是真正理解了斜率优化DP 根据状态转移方程f[i]=max(f[j]+ax^2+bx+c),x=sum[i]-sum[j] 可以变形为 f[i]=max((a*sum[j]^2-b*sum[j])-(2a*sum[j]*sum[i]))+(a*sum[i]^2+b*sum[i]+c) 我们可以把每个决策映射到平面上的一个点 其中x坐标为(a*sum[j]^2-b*sum[j])代表此决策的固定价值(与转移到哪无关) y坐标为-(2a*sum[j]) 代表此决策的潜在价值(