Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7960 Accepted Submission(s): 2465
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.
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
【思路】
斜率优化。
设f[i],则转移式为f[i]=min{f[j]+(C[i]-C[j])^2+M},1<=j<i
进一步得:f[i]=min{ (f[j]+C[j]^2-2*C[i]*C[j])+(C[i]^2+M) }
设y(j)=f[j]+C[j]^2,a[i]=-*C[i],x(j)=C(j),则f[i]=min{y(j)+2*a[i]*x(j)}+C[i]^2+M
则要求min p=y+2ax , 单调队列维护下凸包。
【代码】
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 6 const int N = 500000+10; 7 8 struct point { int x,y; 9 }q[N],now; 10 int L,R,n,m,C[N],f[N]; 11 int cross(point a,point b,point c) { 12 return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); 13 } 14 void read(int& x) { 15 char c=getchar(); while(!isdigit(c)) c=getchar(); 16 x=0; while(isdigit(c)) x=x*10+c-'0' , c=getchar(); 17 } 18 int main() { 19 while(scanf("%d%d",&n,&m)==2) { 20 for(int i=1;i<=n;i++) 21 read(C[i]) , C[i]+=C[i-1]; 22 L=R=0; 23 for(int i=1;i<=n;i++) { 24 while(L<R && q[L].y-2*C[i]*q[L].x>=q[L+1].y-2*C[i]*q[L+1].x) L++; 25 now.x=C[i]; //计算xi 26 now.y=q[L].y-2*C[i]*q[L].x+2*C[i]*C[i]+m; //计算yi=f[i]+b[i]^2 = min p+a[i]^2+b[i]^2+M 27 while(L<R && cross(q[R-1],q[R],now)<=0) R--; 28 q[++R]=now; 29 } 30 printf("%d\n",q[R].y-C[n]*C[n]); 31 } 32 return 0; 33 }