您的位置:首页 > 其它

BZOJ1010 [HNOI2008] 玩具装箱toy

2016-01-07 14:39 351 查看
题目链接: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初探

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define travel(x) for(Edge *p=last[x]; p; p=p->pre)
using namespace std;
typedef long long ll;
const int maxn = 50010;
ll n,L,m,head,tail,q[maxn],f[maxn],s[maxn];
inline double slope(int x,int y){
return (f[y] - f[x] + s[y] * s[y] - s[x] * s[x]) / (s[y] - s[x]);
}
inline ll read(){
ll ans = 0, f = 1;
char c = getchar();
while (!isdigit(c)){
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)){
ans = ans * 10 + c - '0';
c = getchar();
}
return ans * f;
}
int main(){
n = read(); L = read(); s[0] = 0;
rep(i,1,n) s[i] = s[i-1] + read();
rep(i,1,n) s[i] += i;
f[0] = q[0] = head = tail = 0;
rep(i,1,n){
m = s[i] - L - 1;
while (head < tail && slope(q[head+1],q[head]) <= m<<1) head++;
f[i] = f[q[head]] + (m - s[q[head]]) * (m - s[q[head]]);
while (head < tail && slope(q[tail],q[tail-1]) >= slope(i,q[tail])) tail--;
q[++tail] = i;
}
printf("%lld\n",f
);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: