Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 6455 Accepted Submission(s): 1990
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
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; int sum[500010],dp[500010],q[500010]; int gety(int i,int j) { return dp[i]-dp[j]+(sum[i]+sum[j])*(sum[i]-sum[j]); } int getx(int i,int j) { return 2*(sum[i]-sum[j]); } int main() { int n,m; while(cin>>n>>m) { for(int i=1;i<=n;i++) { cin>>sum[i]; sum[i]+=sum[i-1]; } int head(0),tail(1); for(int i=1;i<=n;i++) { while(head+1<tail&&gety(q[head+1],q[head])<=sum[i]*getx(q[head+1],q[head])) head++; dp[i]=dp[q[head]]+(sum[i]-sum[q[head]])*(sum[i]-sum[q[head]])+m; while(head+1<tail&&gety(i,q[tail-1])*getx(q[tail-1],q[tail-2])<=getx(i,q[tail-1])*gety(q[tail-1],q[tail-2])) tail--; q[tail++]=i; } cout<<dp[n]<<endl; } }