玩具装箱 BZOJ 1010

玩具装箱

【问题描述】

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。但他希望费用最小.

【输入格式】

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

【输出格式】

输出最小费用

【样例输入】

5 4

3

4
2
1
4

【样例输出】

1


题解:

设f[i]为选完前i个最小的费用

那么转移方程:

玩具装箱 BZOJ 1010

发现具有决策单调性

那么······

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#define big long long
using namespace std;
struct Ti
{
int x, y, z;
}o[];
int n, m;
big s;
big sum[], f[];
big sqr(big x)
{
return x * x;
}
big Cal(big x, big y)
{
return f[x] + sqr(sum[y] - sum[x] + y - x - - m);
}
int Two(int x, int y, int z, int ss)
{
int l = x, r = y, mi;
while(l <= r)
{
mi = (l + r) >> ;
if(Cal(ss, mi) < Cal(z, mi)) r = mi - ;
else l = mi + ;
}
return l;
}
int main()
{
scanf("%d%d", &n , &m);
for(int i = ; i <= n; ++i)
{
scanf("%lld", &s);
sum[i] = sum[i - ] + s;
}
int t = , w = , cc;
o[] = (Ti) {, n, };
for(int i = ; i <= n; ++i)
{
if(i > o[t].y) ++t;
f[i] = Cal(o[t].z, i);
if(Cal(i, n) < Cal(o[w].z, n))
{
while(t <= w && Cal(i, o[w].x) < Cal(o[w].z, o[w].x)) --w;
if(t <= w)
{
cc = Two(o[w].x, o[w].y, o[w].z, i);
o[w].y = cc - ;
o[++w] = (Ti) {cc, n, i};
}
else o[++w] = (Ti) {i, n, i};
}
}
printf("%lld", f[n]);
}
上一篇:SilverLight:基础控件使用(2)-ComboBox,ListBox控件


下一篇:【Geek软技能】程序员,为什么写不好一份简历?