"The main topic"
Professor P has the number 1 ... N Toys, Part I toy length is ci. For ease of finishing, Professor P requires that the number of toys in a one-dimensional container be continuous. If the toy I toys to the J toy into a container, then the length of the container will be X=j-i+sigma (Ck) i<=k<=j the cost of making the container is related to the length of the container, if the container length is x, the production cost is (X-L) ^2. Where L is a constant. Minimum cost.
Ideas
Too lazy to say, the WC Song Nio Teacher's courseware to carry a bit.
Song Nio Teacher said very well, when the first time to listen to slope optimization of the WC listen to him after the second understand, after a few months to digest.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 Const intmaxn=50000+ -;8 intn,l;9 intC[MAXN];Ten LL S[MAXN],G[MAXN],X[MAXN],Y[MAXN],F[MAXN]; One A ll Square (ll x) - { - returnx*x; the } - - DoubleSlopintIintj) - { + return((Y[j]-y[i])/(x[j]-x[i])); - } + A voidDP () at { -Memset (F,0,sizeof(f)); - intHead=0, tail=0; - intQ[MAXN]; -q[head]=0; - for(intI=1; i<=n;i++) in { -y[i]=f[i-1]+Square (X[i]); to while(head+1<tail && Slop (q[tail-2],q[tail-1]) >slop (q[tail-1],i)) tail--; +q[tail++]=i; - while(head+1<tail && Slop (q[head],q[head+1]) <2.0*g[i]) head++; thef[i]=f[q[head]-1]+square (g[i]-X[q[head]]); * } $ }Panax Notoginseng - voidInit () the { +scanf"%d%d",&n,&l); Amemset (s),0,sizeof(s)); the for(intI=1; i<=n;i++) + { -scanf"%d",&c[i]); $s[i]=s[i-1]+C[i]; $g[i]=s[i]+i-l; -x[i]=s[i-1]+i; - } the } - Wuyi voidPrintans () the { -printf"%lld", F[n]); Wu } - About intMain () $ { - init (); - DP (); - Printans (); A return 0; +}
"Slope optimization" BZOJ1010 [HNOI2008] Toy boxing toy