题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3507
累加器传送门:
http://blog.youkuaiyun.com/noiau/article/details/71775000
题目:
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.
输入:
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.
输出:
A single number, meaning the mininum cost to print the article.
样例输入:
5 5
5
9
5
7
5
样例输出:
230
这篇博客本来写好了的…然后没有保存一下子不小心把浏览器关了…所以重新写一遍吧
分析题目
分割出最小值
所以可以想到一个很简单的dp转移方程
dp[i]=min(dp[i],dp[j]+(cnt[i]-cnt[j])^2+M);
(伪代码)
其中dp[i]表示把前i个数分解成若干段所能获得的最小值,可以枚举j 然后把i到j切成一段需要花费(cnt[i]-cnt[j])^2和一个M
cnt数组维护的是前缀和
把括号打开,M可以直接拿出来
dp[i]=min(dp[i],dp[j]+cnt[i]^2-2*cnt[i]*cnt[j]+cnt[j]^2)+M
M就扔了,最后再加上去,因为M是个常量,无论怎么决策都不影响M的值
而对于一个i来说,cnt[i]^2也一定是确定的
所以再把cnt[i]