Slope optimization. The first time to write ... Copy from Proverbs ...
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define REP (i,n) for (int i=1;i<=n;i++) #define CLR (x,c) memset (x,c,sizeof (x)) #define LL long Longconst int nmax= 50005;const int Inf=0x7f7f7f7f;ll Read () {ll x=0;char C=getchar (); bool F=true;while (!isdigit (c)) {if (c== '-') f=false;c= GetChar ();} while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return f?x:-x;} ll Sum[nmax],f[nmax],dp[nmax],c,n;int q[nmax];void init () {n=read (), C=read (); C++;rep (I,n) Sum[i]=read (), sum[i]+=sum [I-1],f[i]=sum[i]+i;} ll S (int x,int y) {return (f[x]-f[y]);} ll G (int x,int y) {return dp[x]-dp[y]+ (f[x]+c) * (f[x]+c)-(f[y]+c) * (F[Y]+C);} void work () {int H=1,t=2;q[1]=0;rep (i,n) {while (H<t-1&&g (Q[h+1],q[h]) <=s (Q[h+1],q[h]) *f[i]) h++;DP [i] =dp[q[h]]+ (f[i]-f[q[h]]-c) * (F[I]-F[Q[H]]-C), while (H<t-1&&g (I,q[t-1]) *s (q[t-1],q[t-2]) <=G (Q[t-1], Q[t-2]) *s (i,q[t-1]) t--;q[t++]=i;} printf ("%lld\n", Dp[n]);} int main () {init (); work (); return 0;}
1010: [HNOI2008] Toy boxing toy time limit:1 Sec Memory limit:162 MB
submit:8943 solved:3554
[Submit] [Status] [Discuss] Description
Professor P was going to see the Olympics, but he couldn't give up his toys, so he decided to ship all the toys to Beijing. He uses his own compressor to press
It can turn any item into a pile and then into a special one-dimensional container. Professor P has the number 1 ... n Toys, item I Toys
After compression becomes a one-dimensional length of ci. For ease of finishing, Professor P requires that the number of toys in a one-dimensional container be continuous. And if a one-dimensional capacity
There are a number of toys in the device, then two pieces of toys to add a unit length of the filler, in the form of saying that if I toy to the J Toys put to a
container, the length of the container will be X=j-i+sigma (Ck) i<=k<=j the cost of making a container is related to the length of the container, according to the professor,
If the container length is X, its production cost is (X-L) ^2. Where L is a constant. Professor P does not care about the number of containers, he can make any length of the capacity
Even more than L. But he wants the cost to be minimal.
Input
The first line enters two integers n,l. Next N Lines Enter ci.1<=n<=50000,1<=l,ci<=10^7
Output
Minimum cost of output
Sample Input5 4
3
4
2
1
4Sample Output1HINT Source [Submit] [Status] [Discuss]
BZOJ1010: [HNOI2008] Toy packing toy