玩具装箱 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个最小的费用
那么转移方程:

发现具有决策单调性
那么······
#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]);
}
玩具装箱 BZOJ 1010的更多相关文章
- 【BZOJ】【1010】【HNOI2008】玩具装箱Toy
DP/斜率优化 根据题目描述很容易列出动规方程:$$ f[i]=min\{ f[j]+(s[i]-s[j]+i-j-1-L)^2 \}$$ 其中 $$s[i]=\sum_{k=1}^{i} c[k] ...
- 【BZOJ 1010】 [HNOI2008]玩具装箱toy (斜率优化)
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9330 Solved: 3739 Descriptio ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP
1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...
- 【斜率DP】BZOJ 1010:玩具装箱
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7537 Solved: 2888[Submit][St ...
- bzoj 1010 [HNOI2008]玩具装箱toy(DP的斜率优化)
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7874 Solved: 3047[Submit][St ...
- BZOJ 1010 [HNOI2008]玩具装箱toy
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7184 Solved: 2724[Submit][St ...
- Bzoj 1010: [HNOI2008]玩具装箱toy(斜率优化)
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MB Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定 ...
- BZOJ 1010 (HNOI 2008) 玩具装箱
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MB Submit: 12665 Solved: 5540 [Submit][S ...
随机推荐
- 安装linux虚拟机(Ubuntu & KALI)
VMware workstation 15.0.0 ubuntu-18.10-desktop 首先安装VMware 参考资料很多,不再赘述. 之后参考 https://www.cnblogs.com/ ...
- A*和IDA*介绍
\(A*\)算法是一种很神奇的搜索方法,它属于启发式搜索中的一种.A*最主要的功能当然就是用来剪枝,提高搜索的效率.A*主要的实现方法是通过一个估价函数,每次对下一步进行一个估价,根据估价出的值来决定 ...
- vuejs 的错误代码,有助于理解
1.vuejs 的双向绑定理解 2.嵌套复杂对象的使用. * 1. 分割都是, 2. json 中分割都是; 4. v-bind:value="param" 括号内的就是 vuej ...
- HDU-2544-最短路(Bellman-Ford)
Bellman-Ford算法是一个时间复杂度很高,但是它可以用来判断负环 负环就是上面的图,那个环的整体值小于零了,所以就是负环. 我们用Bellman-Ford算法进行更新,打一个表出来: k a ...
- LeetCode(8)String to Integer (atoi)
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Spark MLlib + maven + scala 试水~
使用SGD算法逻辑回归的垃圾邮件分类器 package com.oreilly.learningsparkexamples.scala import org.apache.spark.{SparkCo ...
- bash快捷键及输入输出重定向
bash特性之快捷键: Ctrl+a: 跳转至命令首部 Ctrl+e: 跳转至命令尾部 Ctrl+l: 清屏 Ctrl+c: 中止或取消 Ctr ...
- go 和make的用法 区别
Doand Make are two verbs which frequently confuse students of English. Learn the Difference between ...
- 关于springmvc返回json格式数据
1.引入maven依赖 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifac ...
- joyoi1935 「Poetize3」导弹防御塔
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #i ...