bzoj1010 [HNOI2008]玩具装箱toy

Description

  P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京。他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中。P教授有编号为1...N的N件玩具,第i件玩具经过压缩后变成一维长度为Ci.为了方便整理,P教授要求在一个一维容器中的玩具编号是连续的。同时如果一个一维容器中有多个玩具,那么两件玩具之间要加入一个单位长度的填充物,形式地说如果将第i件玩具到第j个玩具放到一个容器中,那么容器的长度将为 x=j-i+Sigma(Ck) i<=K<=j 制作容器的费用与容器的长度有关,根据教授研究,如果容器长度为x,其制作费用为(X-L)^2.其中L是一个常量。P教授不关心容器的数目,他可以制作出任意长度的容器,甚至超过L。但他希望费用最小.

Input

  第一行输入两个整数N,L.接下来N行输入Ci.1<=N<=50000,1<=L,Ci<=10^7

Output

  输出最小费用

Sample Input

5 4
3
4
2
1
4

Sample Output

1

 

正解:划分型dp+斜率优化。

很容易想出n^2的dp,然后发现可以分成i的状态,j的状态和i*j的状态,那么这就可以用斜率优化做了。

 

 1 //It is made by wfj_2048~
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set>
13 #define inf 1<<30
14 #define il inline
15 #define RG register
16 #define ll long long
17  
18 using namespace std;
19  
20 struct node{ ll x,y; }q[50010];
21  
22 ll f[50010],c[50010],n,l,st,ed;
23  
24 il ll gll(){
25     RG ll x=0,q=0; RG char ch=getchar();
26     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();
27     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
28 }
29  
30 il void work(){
31     n=gll(),l=gll()+1; for (RG ll i=1;i<=n;++i) c[i]=gll()+c[i-1]; for (RG ll i=1;i<=n;++i) c[i]+=i;
32     st=1,ed=2,f[1]=(c[1]-l)*(c[1]-l); q[ed]=(node){2*c[1],f[1]+c[1]*c[1]+2*l*c[1]};
33     for (RG ll i=2;i<=n;++i){
34     RG ll k=c[i]; while (st<ed && (q[st+1].y-q[st].y)<(q[st+1].x-q[st].x)*k) st++;
35     f[i]=(c[i]-l)*(c[i]-l)+q[st].y-k*q[st].x; RG ll X=2*c[i],Y=f[i]+c[i]*c[i]+2*l*c[i];
36     while (st<ed && (Y-q[ed].y)*(q[ed].x-q[ed-1].x)<(q[ed].y-q[ed-1].y)*(X-q[ed].x)) ed--; q[++ed]=(node){X,Y};
37     }
38     printf("%lld",f[n]); return;
39 }
40  
41 int main(){
42     work();
43     return 0;
44 }

 

posted @ 2017-02-10 21:58  wfj_2048  阅读(154)  评论(0编辑  收藏  举报