zoukankan      html  css  js  c++  java
  • 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
    ",f[m][n]/m);
    44     return 0;
    45 }
  • 相关阅读:
    xml
    企业级应用和互联网应用的区别
    javaee学习目标
    数据库基本原理
    数据库学习感想
    数据库设计
    团队项目自我评价
    团队项目-软件度量
    团队项目-初级版本
    团队项目—详细设计
  • 原文地址:https://www.cnblogs.com/guapisolo/p/9697073.html
Copyright © 2011-2022 走看看