[NOI2007]货币兑换Cash(DP+动态凸包)
第一次打动态凸包维护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+动态凸包)的更多相关文章
- 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的数量. 然后就很明显了...平衡 ...
- bzoj1492[NOI2007]货币兑换Cash cdq分治+斜率优化dp
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5541 Solved: 2228[Submit][Sta ...
- BZOJ1492: [NOI2007]货币兑换Cash 【dp + CDQ分治】
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MB Submit: 5391 Solved: 2181 [Submit][S ...
- [BZOJ1492] [NOI2007]货币兑换Cash 斜率优化+cdq/平衡树维护凸包
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5907 Solved: 2377[Submit][Sta ...
- 【BZOJ-1492】货币兑换Cash DP + 斜率优化 + CDQ分治
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 3396 Solved: 1434[Submit][Sta ...
- bzoj千题计划237:bzoj1492: [NOI2007]货币兑换Cash
http://www.lydsy.com/JudgeOnline/problem.php?id=1492 dp[i] 表示 第i天卖完的最大收益 朴素的dp: 枚举从哪一天买来的在第i天卖掉,或者是不 ...
- [BZOJ1492][NOI2007]货币兑换Cash(斜率优化+CDQ分治)
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5838 Solved: 2345[Submit][Sta ...
- 【BZOJ1492】[NOI2007]货币兑换Cash 斜率优化+cdq分治
[BZOJ10492][NOI2007]货币兑换Cash Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下简称B券).每 ...
- 1492: [NOI2007]货币兑换Cash【CDQ分治】
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 4166 Solved: 1736[Submit][Sta ...
- [BZOJ1492] [NOI2007] 货币兑换Cash(cdq分治+斜率优化)
[BZOJ1492] [NOI2007] 货币兑换Cash(cdq分治+斜率优化) 题面 分析 dp方程推导 显然,必然存在一种最优的买卖方案满足:每次买进操作使用完所有的人民币:每次卖出操作卖出所有 ...
随机推荐
- CastleWindsor 使用说明
1.引用DLL Castle.Core.dll 和Castle.Windsor.dll 2. 引用命名空间 using Castle.MicroKernel.Resolvers.Specialize ...
- 使用django-compressor压缩静态文件
Reference:http://blog.csdn.net/permike/article/details/52355095 在网站开发阶段,对于静态资源文件比如JS,CSS等文件都是未经过压缩合并 ...
- readystate, 异步
EventUtil.addHandler(window, "load", function(){ //create a new <script/> element. v ...
- eclipse中创建类和方法自动注释
<?xml version="1.0" encoding="UTF-8"?><templates><template autoin ...
- sklearn数据预处理-scale
对数据按列属性进行scale处理后,每列的数据均值变成0,标准差变为1.可通过下面的例子加深理解: from sklearn import preprocessing import numpy as ...
- Java数据库连接--JDBC调用存储过程,事务管理和高级应用
相关链接:Jdbc调用存储过程 一.JDBC常用的API深入详解及存储过程的调用 1.存储过程的介绍 我们常用的操作数据库语言SQL语句在执行的时候要先进行编译,然后执行,而存储过程是在大型数据库系统 ...
- Spring @Aspect进行类的接口扩展
Spring @Aspect进行类的接口扩展: XML: <?xml version="1.0" encoding="UTF-8"?> <be ...
- margin三个值
http://www.cnblogs.com/wangkongming/p/3204734.html margin标记可以带一个.二个.三个.四个参数,各有不同的含义. margin: 20px;(上 ...
- I帧/P帧/B帧---术语解释
视频压缩中,每帧代表一幅静止的图像.而在实际压缩时,会采取各种算法减少数据的容量,其中IPB就是最常见的. 简单地说,I帧是关键帧,属于帧内压缩.就是和AVI的压缩是一样的. P是向前搜索的意思.B ...
- 导入礼包时,遇到file_get_contents读取的文本,去除空格、换行等的方法
解决方法:正则替换: 代码:$gift_code_str = preg_replace('/((\s)*(\n)+(\s)*) /i ',',',file_get_contents($gift_cod ...