Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 17081 Accepted Submission(s): 5241
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
Recommend
zhengfeng | We have carefully selected several similar problems for you: 3506 3501 3504 3505 3498
【思路】
设dp[i]为打印前i个单词所需要的代价,sum[i]为每个单词代价的前缀和。
可得状态转移方程:dp[i] = min{dp[j] + (sum[i] - sum[j])^2 + M},(j < i),普通的转移方式是O(N^2)的,不能接受。
我们假设在j处转移优于在k处转移,即dp[j] + (sum[i] - sum[j])^2 + M <= dp[k] + (sum[i] - sum[k])^2 + M,化简得不等式:
,令
,令
,
得:,好了,这个式子形式上就是斜率的不等式了。
我们所做的一切都是为了得到一个更快的转移方式,我们当确定一个数i的dp值后,就能推知它对应的点(s(i), f(i)),即一个dp[i]对应一个点。上面的不等式告诉我们:满足此不等式的两个点(s(j), f(j)),(s(k), f(k))中,拿(s(j), f(j))来转移得到(s(i), f(i))更优!由于式子右边是单调递增的,所以我们有理由去掉那些“差劲”的k们,只留下最后的胜利者,即某个点(s(j), f(j))满足与算出状态的其他点的斜率小于等于sum[i]。转移完了以后,我们还要注意,也正是如此整个过程里所有可能的点与它前面的点的连线的斜率只能是单调递增的,和新点不满足此条件的点都要被我们踢出去。
画出来的合法转移的折线正如其他博客所言的下凸包的形状。
【代码】
//******************************************************************************
// File Name: HDU_3507.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年08月05日 星期日 20时46分42秒
//******************************************************************************
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 5e5 + 5;
int n, m, head, tail;
int dq[MAXN];
ll sum[MAXN], dp[MAXN];
ll get_up(int i, int j)
{
return (dp[i] + sum[i] * sum[i]) - (dp[j] + sum[j] * sum[j]);
}
ll get_down(int i, int j)
{
return (ll)2 * (sum[i] - sum[j]);
}
int main()
{
while (scanf("%d %d", &n, &m) == 2) {
sum[0] = dp[0] = 0;
for (int i = 1; i <= n; i++)
scanf("%lld", &sum[i]), sum[i] += sum[i - 1];
head = tail = 0;
dq[tail++] = 0;
for (int i = 1; i <= n; i++) {
while (tail - head >= 2 && get_up(dq[head + 1], dq[head]) <= sum[i] * get_down(dq[head + 1], dq[head])) head++;
dp[i] = dp[dq[head]] + (sum[i] - sum[dq[head]]) * (sum[i] - sum[dq[head]]) + m;
while (tail - head >= 2 && get_up(dq[tail - 1], dq[tail - 2]) * get_down(i, dq[tail - 1]) >= get_up(i, dq[tail - 1]) * get_down(dq[tail - 1], dq[tail - 2])) tail--;
dq[tail++] = i;
}
printf("%lld\n", dp[n]);
}
return 0;
}