首先我们贪心的考虑,对于某一天来说,我们只有3中策略,第一种为不做任何行动,这时的答案与前一天相同,第二种为将自己的钱全部换成a,b货币,因为如果换a,b货币,代表在之后的某一天卖出去后会赚钱,那么当时手中的a,b货币越多,盈利越多,所以全买。第三种策略为将自己的货币全部卖出,贪心正确性和第二种类似。

  那么我们设w[i]为到第i天,手中最多有多少钱,那么就可以比较容易的列出转移方程w[i]=max(w[i-1],第j天将所有的货币卖出,第i天卖掉的钱),这样的时间复杂度为n^2,显然不能通过全部测试数据,那么我们考虑优化。

  w[i-1]比较容易考虑,那么我们现在要求的就是对于固定的i,第j天将所有的货币卖出,第i天卖掉的钱的最大值,设这个为tot,那么我们用式子表示出这个来,因为第j天剩下w[j]的钱,那么我们可以换成a货币w[j]/(a[j]+b[j]*rate[j]),b货币为rate[j]*a货币,那么设x[i]为第i天的钱全部换成a货币的数量,y[i]为第i天的钱全部换成b货币的数量,那么则有x[i]=w[i]/(a[i]+b[i]*rate[i]),y[i]=x[i]*rate[i],那么第j天的货币在第i天卖掉获得的钱为a[i]*x[j]+b[i]+y[j],那么w[i]=max(w[i-1],a[i]*x[j]+b[i]*y[j]),现在我们求的就是z=a[i]*x[j]+b[i]*y[j]的最大值,那么用线性规划的方法,y[j]=z/b[i]-x[j]*a[i]/b[i],显然当使斜率为-a[i]/b[i]的直线的纵截距最大的时候答案最优,那么我们只需要维护一个x,y坐标的下凸壳即可。

  但是这道题的x坐标显然没有什么单调性,所以我们可以用splay来维护。

/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ //By BLADEVIL
const
eps =1e-9; var
n :longint;
w, a, b, rate :array[..] of extended;
x, y :array[..] of extended;
lk, rk :array[..] of extended;
root :longint;
son :array[..,..] of longint;
father :array[..] of longint; function max(a,b:extended):extended;
begin
if a>b then exit(a) else exit(b);
end; procedure init;
var
i :longint;
begin
read(n,w[]);
for i:= to n do read(a[i],b[i],rate[i]);
end; function fabs(x:extended):extended;
begin
if x> then exit(x) else exit(-x);
end; procedure rotate(x:longint;var root:longint);
var
y, z, p, q :longint;
begin
y:=father[x];
z:=father[y];
if son[y][]=x then p:= else p:=;
q:=p xor ;
if y=root then root:=x
else if son[z][]=y then
son[z][]:=x else son[z][]:=x;
father[x]:=z; father[y]:=x;
father[son[x][q]]:=y;
son[y][p]:=son[x][q];
son[x][q]:=y;
end; procedure splay(x:longint;var root:longint);
var
y, z :longint;
begin
while x<>root do
begin
y:=father[x];
z:=father[y];
if y<>root then
if (son[y][]=x) xor (son[z][]=y)
then rotate(x,root) else rotate(y,root);
rotate(x,root);
end;
end; procedure insert(var t:longint;anc,now:longint);
begin
if t= then
begin
t:=now;
father[t]:=anc;
exit;
end;
if (x[now]<=x[t]+eps) then
insert(son[t][],t,now) else
insert(son[t][],t,now);
end; function getk(i,j:longint):extended;
begin
if fabs(x[i]-x[j])<eps then exit(-maxlongint);
exit((y[j]-y[i])/(x[j]-x[i]));
end; function prev(root:longint):longint;
var
rot, tmp :longint;
begin
rot:=son[root][];
tmp:=rot;
while rot<> do
begin
if (getk(rot,root)<=lk[rot]+eps) then
begin
tmp:=rot;
rot:=son[rot][];
end else rot:=son[rot][];
end;
exit(tmp);
end; function succ(root:longint):longint;
var
rot, tmp :longint;
begin
rot:=son[root][];
tmp:=rot;
while rot<> do
begin
if (getk(root,rot)+eps>=rk[rot]) then
begin
tmp:=rot;
rot:=son[rot][];
end else rot:=son[rot][];
end;
exit(tmp);
end; procedure update(t:longint);
var
left, right :longint;
begin
splay(t,root);
if son[t][]<> then
begin
left:=prev(root);
splay(left,son[root][]);
son[left][]:=;
lk[t]:=getk(left,t);
rk[left]:=lk[t];
end else lk[t]:=maxlongint;
if son[t][]<> then
begin
right:=succ(root);
splay(right,son[root][]);
son[right][]:=;
rk[t]:=getk(t,right);
lk[right]:=rk[t];
end else rk[t]:=-maxlongint;
if (lk[t]<=rk[t]+eps) then
begin
root:=son[t][];
son[root][]:=son[t][];
father[son[t][]]:=root;
father[root]:=;
rk[root]:=getk(root,son[t][]);
lk[son[t][]]:=lk[root];
end;
end; function find(t:longint;k:extended):longint;
begin
if t= then exit();
if (lk[t]+eps>=k) and (k+eps>=rk[t]) then exit(t);
if (k+eps>lk[t]) then
exit(find(son[t][],k)) else
exit(find(son[t][],k));
end; procedure main;
var
i :longint;
p :longint;
begin
for i:= to n do
begin
p:=find(root,-a[i]/b[i]);
w[i]:=max(w[i-],x[p]*a[i]+y[p]*b[i]);
y[i]:=w[i]/(a[i]*rate[i]+b[i]);
x[i]:=y[i]*rate[i];
insert(root,,i);
update(i);
end;
writeln(w[n]::);
end; begin
init;
main;
end.

bzoj 1942 斜率优化DP的更多相关文章

  1. bzoj 1010 斜率优化DP

    我的第二道斜率DP. 收获: 1.假设两个位置:p<q<i,然后让某一位置优,看其满足什么性质,所谓斜率优化就是满足: (g[q]-g[p])/(f[q]-f[p])  op h[i] 要 ...

  2. bzoj 1096 斜率优化DP

    首先比较容易的看出来是DP,w[i]为前i个工厂的最小费用,那么w[i]=min(w[j-1]+cost(j,i))+c[i],但是这样是不work的,复杂度上明显过不去,这样我们考虑优化DP. 设A ...

  3. bzoj 3437 斜率优化DP

    写题解之前首先要感谢妹子. 比较容易的斜率DP,设sum[i]=Σb[j],sum_[i]=Σb[j]*j,w[i]为第i个建立,前i个的代价. 那么就可以转移了. /**************** ...

  4. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP

    1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...

  5. BZOJ 3156: 防御准备 斜率优化DP

    3156: 防御准备 Description   Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...

  6. BZOJ 1010: 玩具装箱toy (斜率优化dp)

    Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy(斜率优化dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1010 题意: 思路: 容易得到朴素的递归方程:$dp(i)=min(dp(i),dp(k)+(i-k ...

  8. BZOJ 1010 [HNOI2008]玩具装箱 (斜率优化DP)

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=1010 思路 [斜率优化DP] 我们知道,有些DP方程可以转化成DP[i]=f[j]+x[i ...

  9. 【BZOJ】1911: [Apio2010]特别行动队(斜率优化dp)

    题目 传送门:QWQ 分析 用$ dp[i] $ 表示前 i 个人组成的战斗力之和 然后显然$ dp[i]=Max (  dp[j]+a*(sum[i]-sum[j])^2+b*(sum[i]-sum ...

随机推荐

  1. 【连载】Bootstrap开发漂亮的前端界面之插件开发

    相关文章: 1.<教你用Bootstrap开发漂亮的前端界面> 2.<Bootstrap开发漂亮的前端界面之实现原理> 3.<Bootstrap开发漂亮的前端界面之自定义 ...

  2. Python网络编程(socketserver、TFTP云盘、HTTPServer服务器模型)

    HTTP协议? HTTP是一个应用层协议,由请求和响应构成,是一个标准的客户端服务器模型.HTTP是一个无状态的协议. 通常承载于TCP协议之上,有时也承载于TLS或SSL协议层之上,这个时候,就成了 ...

  3. font and face, 浅探Emacs字体选择机制及部分记录

    缘起 最近因为仰慕org-mode,从vim迁移到了Emacs.偶然发现org-mode中调出的calendar第一行居然没有对齐,排查一下发现是字体的问题.刚好也想改改Emacs的字体,于是我就开始 ...

  4. eclipse 创建Makefile工程生成多个执行文件

    1.创建Makefile工程 2.创建inc src Debug 目录 用于存放头文件源文件 3.编写Makefile 需要在有源文件的目标天剑Makefile文件,如下给出一个生成两个target的 ...

  5. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  6. [android]不解锁刷机

    本人因为误操作进入andriod recovery模式,显示failed to boot 2,致手机无法恢复出厂值, 当时那叫一个郁闷.上论坛搜寻无数,唉让刷底包的无数(在此不解释),万恶的刷底包. ...

  7. Python + OpenCV 实现LBP特征提取

    背景 看了些许的纹理特征提取的paper,想自己实现其中部分算法,看看特征提取之后的效果是怎样 运行环境 Mac OS Python3.0 Anaconda3(集成了很多包,浏览器界面编程,清爽) 步 ...

  8. systemPath

    <dependency>   <groupId>com.aliyun.mns</groupId>   <artifactId>aliyun-sdk-mn ...

  9. 写一篇Hook Driver.

    关于Hook,有一本书讲的比较清楚,最近刚刚看完,<Rootkits: Subverting the Windows Kernel> http://www.amazon.com/Rootk ...

  10. DataGridView过滤功能

    http://www.codeproject.com/Articles/33786/DataGridView-Filter-Popup http://www.cnblogs.com/jaxu/arch ...