BZOJ 4518 [Sdoi2016]征途 (斜率优化DP)

我犯了sb错误然后调了1个小时......队列写错了

斜率k递增,b取最小值,队列维护凸包即可

f[0]的预处理好像有些奇怪???我把inf调大就过了???

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #define il inline
 5 #define ll long long
 6 #define N 3010
 7 #define inf 66666666
 8 using namespace std;
 9 //re
10 int n,que[N];
11 ll m,L,f[N][N],a[N],s[N];
12 int gc()
13 {
14     int rett=0,fh=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')fh=-1;c=getchar();}
16     while(c>='0'&&c<='9'){rett=(rett<<3)+(rett<<1)+c-'0';c=getchar();}
17     return rett*fh;
18 }
19 il ll yy(int i,int k){return f[i][k]+m*m*s[k]*s[k]+2ll*m*L*s[k]+L*L;}
20 il ll kk(int j){return 2ll*m*m*s[j];}
21 
22 
23 int main()
24 {
25     freopen("a.in","r",stdin);
26     n=gc(),m=gc();
27     for(int i=1;i<=n;i++) a[i]=gc(),s[i]=s[i-1]+a[i],L+=a[i];
28     int hd,tl;
29     for(int i=1;i<=n;i++) f[0][i]=inf;
30     for(int i=1;i<=m;i++)
31     {
32         hd=tl=1,que[1]=0;
33         for(int j=0;j<=n;j++)
34         {
35             while(hd+1<=tl&&(yy(i-1,que[hd+1])-kk(j)*s[que[hd+1]])<=(yy(i-1,que[hd])-kk(j)*s[que[hd]]))
36                 hd++;
37             f[i][j]=yy(i-1,que[hd])-kk(j)*s[que[hd]]-2ll*m*L*s[j]+m*m*s[j]*s[j];
38             while(hd+1<=tl&&(yy(i-1,j+1)-yy(i-1,que[tl]))*(s[que[tl]]-s[que[tl-1]])<=(yy(i-1,que[tl])-yy(i-1,que[tl-1]))*(s[j+1]-s[que[tl]]))
39                 tl--;
40             que[++tl]=j+1;
41         }
42     }
43     printf("%lld\n",f[m][n]/m);
44     return 0;
45 }

 

posted @ 2018-09-24 22:33  guapisolo  阅读(120)  评论(0编辑  收藏  举报