---
title: Print Article
tags:
  - Dynamic Programming
  - Monotonic Queue
id: "2479"
categories:
  - [OI, Common Skill]
  - [OI, Data Structure]
date: 2018-03-10 23:30:46
---

## 题目描述

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 $C_i$ 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.

## 题意概述

一篇文章中有$N$个单词，输入第$i$个单词的代价为$C_i$。将连续$k$个单词排在一行的总代价为$\left(\sum_{i=1}^k C_i\right)^2+M$。求输入这篇文章的最小代价。

数据范围：$0 \le N \le 5 \times 10^5, \; 0 \le M \le 1000$。

## 算法分析

令$f_i$表示输入前$i$个单词的最小代价，$s_i=\sum_{j=1}^i C_j$。考虑第$i$个单词的所在行，可以得到转移方程

$$
f_i=\min(f_j+(s_i-s_j)^2+M)
$$

假设$k \lt j \lt i$，且$j$这个决策更优，即

$$
f_j+(s_i-s_j)^2+M \le f_k+(s_i-s_k)^2+M
$$

化简得

$$
\begin{align}
f_j-2s_is_j+s_j^2 &\le f_k-2s_is_k+s_k^2 \\\\
f_j+s_j^2-(f_k+s_k^2) &\le 2s_i(s_j-s_k) \\\\
{f_j+s_j^2-(f_k+s_k^2) \over 2s_j-2s_k} &\le s_i
\end{align}
$$

也就是说，在满足这个不等式时，对于状态$i$，决策$j$比决策$k$更优。

我们知道$s_i$单调不递减，即如果对于状态$i_0$，$k \lt j \lt i$且决策$j$更优，那么对于状态$i_1 \gt i_0$，决策$j$也一定更优，因此我们可以抛弃决策$k$。

对于每个$i$，假设平面中有点$(2s_i, f_i+s_i^2)$。对于不等式左侧这个东西，把它看成平面直角坐标系里直线的斜率。由于要最小化$f_i$，因此需要找到一个$k$使得不存在$k \lt j \lt i$满足不等式。可以发现我们只要用单调队列维护前$(i-1)$个点的下凸壳（斜率单调递增）。这样时间复杂度就是$O(N)$的了。

## 代码

```cpp
#include <algorithm>
#include <cstdio>
#include <cstring>

static int const N = 500005;
int a[N], s[N], f[N], que[N];

int detx(int i, int j) { return s[j] - s[i] << 1; }

int dety(int i, int j) { return f[j] + s[j] * s[j] - f[i] - s[i] * s[i]; }

int main() {
  for (int n, m; ~scanf("%d%d", &n, &m);) {
    for (int i = 1; i <= n; ++i)
      scanf("%d", &a[i]);
    for (int i = 1; i <= n; ++i)
      s[i] = s[i - 1] + a[i];
    int qb = 0, qe = 1;
    for (int i = 1; i <= n; ++i) {
      for (; qb + 1 < qe &&
             dety(que[qb], que[qb + 1]) <= s[i] * detx(que[qb], que[qb + 1]);
           ++qb)
        ;
      f[i] = f[que[qb]] + (s[i] - s[que[qb]]) * (s[i] - s[que[qb]]) + m;
      for (; qb + 1 < qe &&
             dety(que[qe - 2], que[qe - 1]) * detx(que[qe - 1], i) >=
                 dety(que[qe - 1], i) * detx(que[qe - 2], que[qe - 1]);
           --qe)
        ;
      que[qe++] = i;
    }
    printf("%d\n", f[n]);
  }
  return 0;
}
```
