标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7960 Accepted Submission(s): 2465
【思路】
斜率优化。
设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 }
HDU 3507 Print Article(DP+斜率优化)
标签:
原文地址:http://www.cnblogs.com/lidaxin/p/5116577.html