Slope optimization: Because the variance is required by m^2 ... that is, the sum of squares of each distance and the mean and multiply M.
F[I][J] Indicates the minimum sum of squares of J-sections after I am gone.
To find the least squares and then multiply by a M is ok = =
Need to be careful, can stay in situ ...
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cstdlib>5#include <cmath>6 #defineD Double7 #definell Long Long8 using namespacestd;9 Const intmaxn=3023;Ten intI,j,k,n,m,now,pre,l,r; One ll PR[MAXN],V[MAXN]; A intDL[MAXN]; -D f[2][MAXN],V1[MAXN]; - the - intRA,FH;CharRx; -InlineintRead () { -Rx=getchar (), ra=0, fh=1; + while((rx<'0'|| Rx>'9') &&rx!='-') rx=GetChar (); - if(rx=='-') fh=-1, rx=GetChar (); + while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnra*fh; A } atInline D XL (intK1,intK2) {//K1<k2 - return(d) (F[pre][k2]-f[pre][k1]+v[k2]-v[k1])/(pr[k2]-Pr[k1]); - } -Inline d sqr (d x) {returnx*x;} - intMain () { -N=read (), m=read (); in for(i=1; i<=n;i++) pr[i]=pr[i-1]+read (), v[i]=pr[i]*Pr[i]; -D avg= (d) pr[n]/m; to for(i=0; i<=n;i++) f[0][I]=SQR (PR[I]-AVG), v1[i]= (pr[i]-avg); +now=1, pre=0; - for(i=2; i<=m;i++, Swap (Now,pre)) { theL=1, r=0; * for(j=0; j<=n;j++){ $ while(L<R&&XL (dl[r-1],dl[r]) >=XL (dl[r],j)) r--;Panax Notoginsengdl[++r]=J; - while(l<r&&2*V1[J]>=XL (dl[l],dl[l+1])) l++; theF[NOW][J]=F[PRE][DL[L]]+SQR (v1[j]-Pr[dl[l]]); + } A } theprintf"%lld\n", (LL) (f[pre][n]*m+0.5)); + return 0; -}
View Code
[bzoj4518] [Sdoi2016] Journey