「HDU3507」Print Article - 斜率优化DP

Link: HDU3507

题面

题面大意

对于长度为 n 的序列 \{a\} , 将其分为几段, 对于段 [l,r] 的花费为 \left(\sum_{i=l}^r a_i\right)^2+M , 求最小花费

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

\left( \sum _ { i = 1 } ^ { k } C _ { i } \right) ^ { 2 } + M

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

1
2
3
4
5
6
5 5
5
9
5
7
5

Sample Output

1
230

Author

Xnozero

Source

2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT

题解

首先写出特征方程:

f_i 表示 [1,i] 的最小花费, sum_i 表示前缀和, 那么有

f_i = \min _ { 1 \leq k < i } \left\{ f_k + ( \operatorname { sum }_i - \operatorname { sum }_k ) ^ { 2 } + M\right\}

直接算就是 O(n^2) 的, 考虑进行优化

对于 dp_i 考虑当在 j 处转移比 k 处更优时, 即

\begin{align}dp_j+(sum_i-sum_j)^2 + M &< dp_k+(sum_i-sum_k)^2 + M\\dp_j+sum_j^2-2\times sum_i\times sum_j&<dp_k+sum_k^2-2\times sum_i\times sum_k\end{align}

也就是

\begin{align} 2 \times sum_i \times ( sum_j - sum_k ) &> dp_j + \operatorname { sum }_j ^ { 2 } - dp_k - \operatorname { sum }_k ^ { 2 }\\2\times sum_i&>{(dp_j+sum_j^2)-(dp_k-sum_k^2)\over sum_j - sum_k}\end{align}

右面这个式子好像一个斜率的形式 (

于是, 我们令 f_i=dp_i+sum_i^2 , 上式就变成了

\begin{align} 2\times sum_i&>{(dp_j+sum_j^2)-(dp_k-sum_k^2)\over sum_j - sum_k}\\2\times sum_i&>{f_j-f_k\over sum_j - sum_k}\end{align}

然后就直接单调队列维护下凸壳就好了

如果担心爆精度就用叉积就好了ww

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstring>
#include <cstdio>

#define int long long

const int MAXN = 1000000 + 10;

struct Point {
int x, y;

Point () {}
Point (int _x, int _y) : x(_x) , y(_y) {}
Point (const Point &t) : x(t.x) , y(t.y) {}
};

typedef Point Vector;

// 叉积
inline int product (Vector a, Vector b) {
return a.x * b.y - a.y * b.x;
}

// 在 k 处转移是否更优
inline bool check (Point i, Point j, Point k) {
Vector ki = Vector(k.x - i.x, k.y - i.y);
Vector kj = Vector(k.x - j.x, k.y - j.y);
return product(ki, kj) <= 0;
}

// 单调队列
int head = 1, tail = 0;
Point que[MAXN];

// 前面不合法的 pop 掉
inline void pop (int limit) {
while (head < tail && (que[head + 1].y - que[head].y) <= limit * (que[head + 1].x - que[head].x)) head++;
}

// 加入新的元素并维护单调性
inline void push (Point x) {
while (head < tail && check(que[tail - 1], que[tail], x)) tail--;
que[++tail] = x;
}

int val[MAXN];
int sum[MAXN];

int dp[MAXN];

signed main () {
int n, m;
while (scanf("%lld%lld", &n, &m) != EOF) {
memset(que, 0, sizeof que);
head = 1, tail = 0;
for (int i = 1; i <= n; i++) scanf("%lld", val + i);
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + val[i]; // 前缀和
push(Point(0, 0));
dp[0] = 0;
for (int i = 1; i <= n; i++) {
pop(2 * sum[i]);
Point now(que[head]);
dp[i] = now.y - now.x * now.x + m + (sum[i] - now.x) * (sum[i] - now.x);
push(Point(sum[i], dp[i] + sum[i] * sum[i]));
}
printf("%lld\n", dp[n]);
}
return 0;
}
坚持原创技术分享,您的支持将鼓励我继续创作!