第一次打动态凸包维护dp,感觉学到了超级多的东西。

首先,set是如此的好用!!!可以通过控制一个flag来实现两种查询,维护凸包和查找斜率k

不过就是重载运算符和一些细节方面有些恶心,90行解决

后面还有一个cdq分治,找时间学下,看下能不能处理一大类恶心的问题

github还是不会用,找时间搞下吧

CODE:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
using namespace std;
const double esp=1e-;
const int maxn=;
int dcmp(double a) {
if (fabs(a)<esp) return ;
return a<?-:;
}
struct node{
double x,y,k;bool f;
node(double _x,double _y):x(_x),y(_y),f(){}
node():f(){}
inline bool error(){return x>1e50;}
}error(1e51,);
inline bool operator == (const node x,const node y) {return dcmp(x.x-y.x)==;}
inline bool operator < (const node x,const node y){
if (x.f||y.f) return dcmp(x.k-y.k)>;
else return dcmp(x.x-y.x)<;
}
inline double operator *(const node x,const node y) {return x.x*y.y-x.y*y.x;}
inline node operator -(const node x,const node y){
node tmp;
tmp.x=x.x-y.x;tmp.y=x.y-y.y;
return tmp;
}
inline double calc_k(node x,node y) {return (x.y-y.y)/(x.x-y.x);}
set<node> gap;
typedef set<node>::iterator iter;
inline node lower(node x) {
iter it=gap.lower_bound(x);
return it==gap.end()?error:*it;
}
inline node next(node x) {
iter it=gap.upper_bound(x);
return it==gap.end()?error:*it;
}
inline node pre(node x) {
iter it=gap.lower_bound(x);
return it==gap.begin()?error:*(--it);
}
inline void ins(node a) {
if (gap.empty()) {a.k=;gap.insert(a);return;}
node d1=pre(a),d2=lower(a);
if (d1.error()&&d2.y>a.y) return ;
if ((!d1.error())&&(!d2.error())&&(dcmp((d2-d1)*(a-d1))<=)) return ;
if (d2==a) return;
node p1,p2=next(a);
for (;;){
p1=p2;p2=next(p2);
if (p1.error()||p2.error()) break;
if (dcmp((p1-a)*(p2-a))<=) break;
gap.erase(p1);
}
p2=pre(a);
for (;;){
p1=p2;p2=pre(p2);
if (p1.error()||p2.error()) break;
if (dcmp((p1-a)*(p2-a))>=) break;
gap.erase(p1);
}
d1=pre(a),d2=next(a);
if (d1.error()) a.k=;else a.k=calc_k(d1,a);gap.insert(a);
if (!d2.error()) gap.erase(d2),d2.k=calc_k(a,d2),gap.insert(d2);
}
inline double get_k(double a,double b) {
node tmp;tmp.f=;tmp.k=-a/b;
tmp=*(--gap.lower_bound(tmp));
return a*tmp.x+b*tmp.y;
}
double a[maxn],b[maxn],r[maxn],na[maxn],nb[maxn],f[maxn];
int main(){
int n,s;
scanf("%d%d",&n,&s);
for (int i=;i<=n;i++) scanf("%lf%lf%lf",a+i,b+i,r+i);
f[]=s;
nb[]=f[]/(a[]*r[]+b[]);
na[]=nb[]*r[];
ins(node(na[],nb[]));
for (int i=;i<=n;i++) {
f[i]=max(f[i-],get_k(a[i],b[i]));
nb[i]=f[i]/(a[i]*r[i]+b[i]);
na[i]=nb[i]*r[i];
ins(node(na[i],nb[i]));
}
printf("%.3f\n",f[n]);
}

  

[NOI2007]货币兑换Cash(DP+动态凸包)的更多相关文章

  1. BZOJ 1492: [NOI2007]货币兑换Cash( dp + 平衡树 )

    dp(i) = max(dp(i-1), x[j]*a[i]+y[j]*b[i]), 0<j<i. x, y表示某天拥有的最多钱去买金券, 金券a和金券b的数量. 然后就很明显了...平衡 ...

  2. bzoj1492[NOI2007]货币兑换Cash cdq分治+斜率优化dp

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 5541  Solved: 2228[Submit][Sta ...

  3. BZOJ1492: [NOI2007]货币兑换Cash 【dp + CDQ分治】

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 5391  Solved: 2181 [Submit][S ...

  4. [BZOJ1492] [NOI2007]货币兑换Cash 斜率优化+cdq/平衡树维护凸包

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 5907  Solved: 2377[Submit][Sta ...

  5. 【BZOJ-1492】货币兑换Cash DP + 斜率优化 + CDQ分治

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 3396  Solved: 1434[Submit][Sta ...

  6. bzoj千题计划237:bzoj1492: [NOI2007]货币兑换Cash

    http://www.lydsy.com/JudgeOnline/problem.php?id=1492 dp[i] 表示 第i天卖完的最大收益 朴素的dp: 枚举从哪一天买来的在第i天卖掉,或者是不 ...

  7. [BZOJ1492][NOI2007]货币兑换Cash(斜率优化+CDQ分治)

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 5838  Solved: 2345[Submit][Sta ...

  8. 【BZOJ1492】[NOI2007]货币兑换Cash 斜率优化+cdq分治

    [BZOJ10492][NOI2007]货币兑换Cash Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下简称B券).每 ...

  9. 1492: [NOI2007]货币兑换Cash【CDQ分治】

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 4166  Solved: 1736[Submit][Sta ...

  10. [BZOJ1492] [NOI2007] 货币兑换Cash(cdq分治+斜率优化)

    [BZOJ1492] [NOI2007] 货币兑换Cash(cdq分治+斜率优化) 题面 分析 dp方程推导 显然,必然存在一种最优的买卖方案满足:每次买进操作使用完所有的人民币:每次卖出操作卖出所有 ...

随机推荐

  1. 结合实际项目分析pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  2. Struts2的那些小细节

    1.result中传多个参数,&不允许使用需要转义&即多个参数之间用&隔开 <result type="redirectAction">show ...

  3. constrain to margins

    如果你点了constrain to margins,左右会有8个点的空挡,而是从8个点后开始计算约束,而没有点时,已屏幕的0点开始计算.

  4. 利用 Grunt (几乎)无痛地做前端开发 (一)之单元测试

    前言 如果你想开发一个js应用,甭管多简单,都要先创建整个宇宙 来看看我们的Javascript小宇宙: 确定如何根据需求.功能划分模块,如何将代码分成多个文件开发,合成一个发布 保证上一条的同时,使 ...

  5. 让表格table呈现边框,不用给tr、td加边框的写法

    <table width="100%" cellspacing="1" cellpadding="1" bgcolor="# ...

  6. C++ 常量类型 const 详解

    1.什么是const? 常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的.(当然,我们可以偷梁换柱进行更新:) 2.为什么引入const? const 推出的初始目的 ...

  7. Flex性能调优相关的一些总结

    1.Performace包含4点:(1)Latency反应时间(2)Scalability:可伸缩性(3)Reliablity:稳定性(4)Availability:可用性2.运行时生命周期:Flex ...

  8. selenium相关面试题

    selenium中如何判断元素是否存在? selenium中hidden或者是display = none的元素是否可以定位到? selenium中如何保证操作元素的成功率?也就是说如何保证我点击的元 ...

  9. Awesome Chrome 插件集锦

    子曾曰:"工欲善其事,必先利其器.居是邦也."--语出<论语·卫灵公>:其后一百多年,荀子也在其<劝学>中倡言道:"吾尝终日而思矣,不如须臾之所学 ...

  10. PHP 7.1 新特性

    PHP 7.1 新特性 1.密集阵算法 2.php int64位支持(2GB的字符串和2GB的文件的上传) 3.$a<=>$b  操作符,排序时有用 4.标量的支持,如果声明int传入st ...