BZOJ1010 [HNOI2008] 玩具装箱toy

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1010

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

输出最小费用

斜率优化DP初探

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #define rep(i,l,r) for(int i=l; i<=r; i++)
 6 #define clr(x,y) memset(x,y,sizeof(x))
 7 #define travel(x) for(Edge *p=last[x]; p; p=p->pre)
 8 using namespace std;
 9 typedef long long ll;
10 const int maxn = 50010;
11 ll n,L,m,head,tail,q[maxn],f[maxn],s[maxn];
12 inline double slope(int x,int y){
13     return (f[y] - f[x] + s[y] * s[y] - s[x] * s[x]) / (s[y] - s[x]);
14 }
15 inline ll read(){
16     ll ans = 0, f = 1;
17     char c = getchar();
18     while (!isdigit(c)){
19         if (c == '-') f = -1;
20         c = getchar();
21     }
22     while (isdigit(c)){
23         ans = ans * 10 + c - '0';
24         c = getchar();
25     }
26     return ans * f;
27 }
28 int main(){
29     n = read(); L = read(); s[0] = 0;
30     rep(i,1,n) s[i] = s[i-1] + read();
31     rep(i,1,n) s[i] += i;
32     f[0] = q[0] = head = tail = 0;
33     rep(i,1,n){
34         m = s[i] - L - 1;
35         while (head < tail && slope(q[head+1],q[head]) <= m<<1) head++;
36         f[i] = f[q[head]] + (m - s[q[head]]) * (m - s[q[head]]);
37         while (head < tail && slope(q[tail],q[tail-1]) >= slope(i,q[tail])) tail--;
38         q[++tail] = i;
39     }
40     printf("%lld\n",f[n]);
41     return 0;
42 }
View Code

 

posted on 2016-01-07 14:39  ACMICPC  阅读(175)  评论(0编辑  收藏  举报

导航