[luogu3195 HNOI2008] 玩具装箱TOY (斜率优化dp)
2024-09-25 03:50:53
题目描述
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
输出格式:
输出最小费用
输入输出样例
输入样例#1:
5 4
3
4
2
1
4
输出样例#1:
1
简单的斜率优化基础题 式子有点长(懒得打)
code:
//By Menteur_Hxy
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <ctime>
#define M(a,b) memset(a,(b),sizeof(a))
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define LL long long
using namespace std;
inline LL rd() {
LL x=0,fla=1; char c=' ';
while(c>'9'|| c<'0') {if(c=='-') fla=-fla; c=getchar();}
while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
return x*fla;
}
inline void out(LL x){
int a[25],wei=0;
if(x<0) putchar('-'),x=-x;
for(;x;x/=10) a[++wei]=x%10;
if(wei==0){ puts("0"); return;}
for(int j=wei;j>=1;--j) putchar('0'+a[j]);
putchar('\n');
}
const int N=50010;
const int INF=0x3f3f3f3f;
int n,L;
LL da[N],f[N],s[N],q[N];
double slope(LL k,LL j) {
return (double) (f[j]-f[k]+(da[j]+L)*(da[j]+L)-(da[k]+L)*(da[k]+L))/(2.0*(da[j]-da[k]));
}
int main() {
n=rd();L=rd();L++;
F(i,1,n) da[i]=rd(),da[i]+=da[i-1]+1;
// F(i,1,n) cout<<da[i]<<" ";cout<<endl;
int h=0,t=0;
F(i,1,n) {
while(h<t && slope(q[h],q[h+1])<=da[i]) h++;
int v=q[h];
f[i]=f[v]+(da[i]-da[v]-L)*(da[i]-da[v]-L);
while(h<t && slope(q[t],i)<slope(q[t-1],q[t])) t--;
q[++t]=i;
}
// F(i,1,n) cout<<f[i]<<" ";cout<<endl;
out(f[n]);
return 0;
}
最新文章
- 响应式web布局中iframe的自适应
- .NET Framework介绍
- Python入门笔记(17):错误、异常
- 2015安徽省赛 C.LU的困惑
- 终于有人把P2P、P2C、O2O、B2C、B2B、C2C 的区别讲透了
- Ajax获得站点文件内容实例
- jenkins 设置权限后管理员登陆提示:Access Denied admin没有Overall/Read权限
- hibernate里的generator中class =value介绍
- 解决将/etc/passwd文件中1000改为0后只能guest进入系统的问题 ||ubuntu下将普通用户权限升级为root用户权限的方法;
- SpringBoot 启动概述
- 【算法】串的模式匹配算法(KMP)
- express中session的使用
- 安装Elasticsearch中Head插件并使用
- [USACO09JAN]Total Flow
- 利用 LibWebP-NET 解码与编码 WebP 格式图片
- android中解决“Dex Loader] Unable to execute dex: Multiple dex files define LXXX”错误
- Gson(Google)基础
- 转载-lvs官方文档-Linux服务器集群系统(二)
- phpexcel 读取日期的问题?
- LongAdder源码学习