BZOJ1010 [HNOI2008] 玩具装箱toy
2016-01-07 14:39
351 查看
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1010
斜率优化DP初探
View Code
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^7Output
输出最小费用斜率优化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
相关文章推荐
- 网页模仿
- VC程序移植到Linux下注意事项
- JCF框架源码分析系列-开头篇(一)
- java synchronized详解
- iOS精益求精 设计模式
- 详解Linux内核之双向循环链表
- Android NDK开发之 ARM与NEON指令说明
- 批量裁剪GIS数据(包含GDB,MDB,Shp)
- Struts2中的ModelDriven机制及其运用
- 解决div下放img标签,会出现空隙的情况
- SQL Server中Text和varchar(max)数据类型区别
- 数据结构-数组详解例子
- matlab的m程序转执行文件exe
- Sicily 1390. Surprising Strings
- asp.net C# Sql 查找用户是否存在
- hadoop入门--简单的MapReduce案例
- [leedcode oj 104]Maximum Depth of Binary Tree
- 高管必看的五大互联网营销方法
- sql server 2008怎么找到隐藏的对象资源管理器
- 关于inflate时LayoutParams不生效的问题