HDU 3507 Print Article(DP+斜率优化)

时间:2021-12-16 10:20:53

              

               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
HDU 3507 Print Article(DP+斜率优化)
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 , 单调队列维护下凸包。

【代码】

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int N = +; struct point { int x,y;
}q[N],now;
int L,R,n,m,C[N],f[N];
int cross(point a,point b,point c) {
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
void read(int& x) {
char c=getchar(); while(!isdigit(c)) c=getchar();
x=; while(isdigit(c)) x=x*+c-'' , c=getchar();
}
int main() {
while(scanf("%d%d",&n,&m)==) {
for(int i=;i<=n;i++)
read(C[i]) , C[i]+=C[i-];
L=R=;
for(int i=;i<=n;i++) {
while(L<R && q[L].y-*C[i]*q[L].x>=q[L+].y-*C[i]*q[L+].x) L++;
now.x=C[i]; //计算xi
now.y=q[L].y-*C[i]*q[L].x+*C[i]*C[i]+m; //计算yi=f[i]+b[i]^2 = min p+a[i]^2+b[i]^2+M
while(L<R && cross(q[R-],q[R],now)<=) R--;
q[++R]=now;
}
printf("%d\n",q[R].y-C[n]*C[n]);
}
return ;
}