题面

题面大意

对于长度为 的序列 , 将其分为几段, 对于段 的花费为 , 求最小花费

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

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

5 5
5
9
5
7
5

Sample Output

230

Author

Xnozero

Source

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

题解

首先写出特征方程:

表示 的最小花费, 表示前缀和, 那么有

直接算就是 的, 考虑进行优化

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

也就是

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

于是, 我们令 , 上式就变成了

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

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

#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;
}