【BZOJ】1096 [ZJOI2007]仓库建设
【算法】DP+斜率优化
【题解】状态转移方程:f[i]=min(f[j]+g(i+1,j-1))+c[i]
关键在于如何O(1)计算g(i+1,j-1)。
推导过程:http://blog.csdn.net/PoPoQQQ/article/details/40504949
当d(j,k)中j<k且k更优时,得到斜率不等式:
(f[j]-f[k]+sumpx[j]-sumpx[k])/(sump[j]-sump[k])<x[i]
于是斜率优化。
斜率优化:http://www.cnblogs.com/onioncyc/p/6113450.html
【细节】
1.是while不是if。。。
2.p[i]*x[i]*1ll。。。
3.转double运算优先度高于除法的样子。
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=;
int q[maxn],x[maxn],p[maxn],c[maxn],n;
ll f[maxn],sump[maxn],sumpx[maxn];
double calc(int j,int k)
{return (double)(f[j]-f[k]+sumpx[j]-sumpx[k])/(sump[j]-sump[k]);}//important3
ll g(int i,int j)
{return x[i]*(sump[i-]-sump[j])-(sumpx[i-]-sumpx[j]);}
int main()
{
scanf("%d",&n);
sump[]=sumpx[]=;
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&x[i],&p[i],&c[i]);
sump[i]=sump[i-]+p[i];
sumpx[i]=sumpx[i-]+1ll*p[i]*x[i];//important2
}
int head=,tail=;q[]=;f[]=;
for(int i=;i<=n;i++)
{
while(head<tail&&calc(q[head],q[head+])<x[i])head++;
f[i]=f[q[head]]+g(i,q[head])+c[i];
while(head<tail&&calc(q[tail-],q[tail])>calc(q[tail],i))tail--;//important1
q[++tail]=i;
}
printf("%lld",f[n]);
return ;
}
【BZOJ】1096 [ZJOI2007]仓库建设的更多相关文章
- BZOJ 1096: [ZJOI2007]仓库建设 [斜率优化DP]
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4201 Solved: 1851[Submit][Stat ...
- bzoj 1096: [ZJOI2007]仓库建设 斜率優化
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2242 Solved: 925[Submit][Statu ...
- bzoj 1096 [ZJOI2007]仓库建设(关于斜率优化问题的总结)
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3234 Solved: 1388[Submit][Stat ...
- BZOJ 1096: [ZJOI2007]仓库建设( dp + 斜率优化 )
dp(v) = min(dp(p)+cost(p,v))+C(v) 设sum(v) = ∑pi(1≤i≤v), cnt(v) = ∑pi*xi(1≤i≤v), 则cost(p,v) = x(v)*(s ...
- 边坡优化主题5——bzoj 1096 [ZJOI2007]仓库建设 解决问题的方法
[原标题] 1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1998 Solved: 816 [id=10 ...
- BZOJ 1096 [ZJOI2007]仓库建设(斜率优化DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1096 [题目大意] 有个斜坡,有n个仓库,每个仓库里面都有一些物品,物品数目为p,仓库 ...
- BZOJ 1096 ZJOI2007 仓库建设 边坡优化
标题效果:特定n植物,其中一些建筑仓库,有一点使,假设没有仓库仓库向右仓库.最低消费要求 非常easy边坡优化--在此之前刷坡优化的情况下,即使这道题怎么错过 订购f[i]作为i点建设化妆i花费所有安 ...
- ●BZOJ 1096 [ZJOI2007]仓库建设
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1096 题解: 斜率优化DP $(d_i:i 位置到1位置的距离,p_i:i位置的成品数量,c ...
- BZOJ 1096 [ZJOI2007]仓库建设:斜率优化dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1096 题意: 有n个工厂,从左往右排成一排,分别编号1到n. 每个工厂里有p[i]件产品, ...
- bzoj 1096: [ZJOI2007]仓库建设【斜率优化】
好眼熟啊 直接dp显然很难算,所以设val为只在n点建一个仓库的费用,然后设f[i]为在i~n点建若干仓库并且i点一定建一个仓库的最大省钱数 转移很显然,设s为p的前缀和,f[i]=max{f[j]+ ...
随机推荐
- YaoLingJump开发者日志(七)
LGame用起来真是各种蛋疼,插背景都可以显示不出来.在屏幕结束后释放资源,重载该屏幕时再setbackground也不行,直接用Lpaper当background更不行,会把tilemap上的东 ...
- linux后台运行之screen和nohup
3.1 nohup命令 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令. 该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. nohup就是不挂起的意 ...
- JAVA学习之HashCode
public native int hashCode(); 返回该对象的哈希码值.支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能. 一.HashCode ...
- [OS] 操作系统-进程线程-经典面试笔试题
题目转自:http://blog.csdn.net/morewindows/article/details/7392749 ·线程的基本概念.线程的基本状态及状态之间的关系? 线程,有时称为轻量级进程 ...
- jQuery实现三级联动
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- ICommand接口
WPF 中的命令是通过实现 ICommand 接口创建的.ICommand 的 WPF 实现是 RoutedCommand 类. WPF 中的主要输入源是鼠标.键盘.墨迹和路由命令.更加面向设备的输入 ...
- 按照list中实体类的某一属性排序
传进一个装有实体类的list public void sort(List<MedicalPracticesDetail> mpdList){ Collections.sort(mpdLis ...
- 【hdu4734】F(x) 数位dp
题目描述 对于一个非负整数 $x=\overline{a_na_{n-1}...a_2a_1}$ ,设 $F(x)=a_n·2^{n-1}+a_{n-1}·2^{n-2}+...+a_2·2^1+ ...
- 【bzoj4580】[Usaco2016 Open]248 区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...
- BZOJ4260 Codechef REBXOR(trie)
用trie求出前缀最大区间异或和.后缀最大区间异或和即可.注意空间是nlog的. #include<iostream> #include<cstdio> #include< ...