BZOJ 1492: [NOI2007]货币兑换Cash [CDQ分治 斜率优化DP]
题意:不想写...
好吧我回来了
首先发现每次兑换一定是全部兑换,因为你兑换说明有利可图,是为了后面的某一天两种卷的汇率差别明显而兑换
那么一定拿全利啊,一定比多天的组合好
$f[i]$表示第$i$天最多能得到的钱在这一天可以换成多少$A$卷
枚举使用哪一天留下的卷,按这一天的汇率换成钱来更新最大钱数
再用这个钱数更新$f[i]$
这样是$O(n^2)$的
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int N=1e5+,M=1e4+;
const double eps=1e-;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,s;
double a[N],b[N],r[N];
double f[N];
void dp(){
f[]=s*r[]/(a[]*r[]+b[]);
double t=s;
for(int i=;i<=n;i++){
for(int j=;j<i;j++) t=max(t,f[j]*a[i]+f[j]/r[j]*b[i]);
f[i]=max(f[i],t*r[i]/(a[i]*r[i]+b[i]));
}
printf("%.3lf",t);
}
int main(){
freopen("in","r",stdin);
n=read();s=read();
for(int i=;i<=n;i++) scanf("%lf%lf%lf",&a[i],&b[i],&r[i]);
dp();
}
DP-naive
然后发现这个式子可以斜率优化
假设转移$j$比$k$更优,且$f_j<f_k$
令$g_i=\frac{f_i}{r_i}$
$\frac{g_k-g_j}{f_k-f_j}\ <\ -\frac{a_i}{b_i}$
然后$f$不单调,所以用平衡树或者CDQ分治来维护
$CDQ$分治里左面按$x$排序,右面按$k$排序
注意:
CDQ分治中$l$和$1$一定别打错.........我$Debug$了好长时间
比较斜率的时候要$+eps$,精度太玄学了呜呜呜
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=1e5+;
const double eps=1e-;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n;
double d[N];
struct Day{
double a,b,r,k,x,y;
int id;
bool operator <(const Day &r)const{return k>r.k;}
}p[N],t[N];
inline bool cmp(Day &a,Day &b){//a<b
return a.x<b.x||(abs(a.x-b.x)<eps&&a.y<b.y);
}
inline double slope(int a,int b){
if(abs(p[a].x-p[b].x)<eps) return 1e20;
else return (p[a].y-p[b].y)/(p[a].x-p[b].x);
}
int st[N],top;
void Solve(int l,int r){//printf("Solve %d %d\n",l,r);
if(l==r){
d[l]=max(d[l],d[l-]);
p[l].y=d[l]/(p[l].a*p[l].r+p[l].b);
p[l].x=p[l].y*p[l].r;
return;
}
int mid=(l+r)>>,p1=l,p2=mid+;
for(int i=l;i<=r;i++){
if(p[i].id<=mid) t[p1++]=p[i];
else t[p2++]=p[i];
}
for(int i=l;i<=r;i++) p[i]=t[i]; Solve(l,mid);
top=;
for(int i=l;i<=mid;i++){
while(top>&&slope(st[top-],st[top])<slope(st[top-],i)+eps) top--;
st[++top]=i;//printf("st %d\n",i);
}
//
int j=;
for(int i=mid+;i<=r;i++){
while(j<top&&slope(st[j],st[j+])+eps>p[i].k) j++;
d[p[i].id]=max(d[p[i].id],p[st[j]].x*p[i].a+p[st[j]].y*p[i].b);
}
Solve(mid+,r);
p1=l;p2=mid+;
for(int i=l;i<=r;i++){
if(p1<=mid&&( p2>r||cmp(p[p1],p[p2]) )) t[i]=p[p1++];
else t[i]=p[p2++];
}
for(int i=l;i<=r;i++) p[i]=t[i];
}
int main(){
//freopen("in","r",stdin);
freopen("cash.in","r",stdin);
freopen("cash.out","w",stdout);
n=read();d[]=read();
for(int i=;i<=n;i++)
scanf("%lf%lf%lf",&p[i].a,&p[i].b,&p[i].r),
p[i].k=-p[i].a/p[i].b,p[i].id=i;
sort(p+,p++n);
Solve(,n);
//for(int i=1;i<=n;i++) printf("hi %d %d %lf\n",i,p[i].id,d[i]);
printf("%.3lf",d[n]);
return ;
}
BZOJ 1492: [NOI2007]货币兑换Cash [CDQ分治 斜率优化DP]的更多相关文章
- bzoj1492[NOI2007]货币兑换Cash cdq分治+斜率优化dp
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5541 Solved: 2228[Submit][Sta ...
- bzoj 1492: [NOI2007]货币兑换Cash【贪心+斜率优化dp+cdq】
参考:http://www.cnblogs.com/lidaxin/p/5240220.html 虽然splay会方便很多,但是懒得写,于是写了cdq 首先要想到贪心的思路,因为如果在某天买入是能得到 ...
- [BZOJ1492] [NOI2007] 货币兑换Cash(cdq分治+斜率优化)
[BZOJ1492] [NOI2007] 货币兑换Cash(cdq分治+斜率优化) 题面 分析 dp方程推导 显然,必然存在一种最优的买卖方案满足:每次买进操作使用完所有的人民币:每次卖出操作卖出所有 ...
- BZOJ1492:[NOI2007]货币兑换 (CDQ分治+斜率优化DP | splay动态维护凸包)
BZOJ1492:[NOI2007]货币兑换 题目传送门 [问题描述] 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和B纪念券(以下简称B券).每个持有金券的 ...
- BZOJ 1492 [NOI2007]货币兑换Cash (CDQ分治/splay 维护凸包)
题目大意:太长了略 splay调了两天一直WA弃疗了 首先,我们可以猜一个贪心,如果买/卖,就一定都买/卖掉,否则不买/卖 反正货币的行情都是已知的,没有任何风险,所以肯定要选择最最最优的方案了 容易 ...
- BZOJ 3963 HDU3842 [WF2011]MachineWorks cdq分治 斜率优化 dp
http://acm.hdu.edu.cn/showproblem.php?pid=3842 写的check函数里写的<但是应该是<=,调了一下午,我是个zz. 就是普通的斜率优化因为有两 ...
- 【uoj#244】[UER #7]短路 CDQ分治+斜率优化dp
题目描述 给出 $(2n+1)\times (2n+1)$ 个点,点 $(i,j)$ 的权值为 $a[max(|i-n-1|,|j-n-1|)]$ ,找一条从 $(1,1)$ 走到 $(2n+1,2n ...
- 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的数量. 然后就很明显了...平衡 ...
- ●BZOJ 1492 [NOI2007]货币兑换Cash
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1492 题解: 斜率优化DP,CDQ分治 定义$DP[i]$为第i天结束后的最大收益. 由于题 ...
随机推荐
- HDU--2115
I Love This Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 访问taotao-portal 中controller中返回taotaoresult 测试httppost方法 出现406错误
方案:1.检查jackson包是否存在 @controller @RequestMapping(value = "/httpclient/post",method=RequestM ...
- eclipse中常用提高效率的快捷键
Eclipse快捷键 10个最有用的快捷键 5 4 Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的 ...
- [学习OpenCV攻略][004][播放AVI视频]
cvCreateFileCapture(文件路径) 创建一个影音文件录像机,返回值为CvCapture类型,用于读取视频文件 cvQuerFrame(视频) 将下一帧视频文件载入内存,当CvCaptu ...
- 阻止安卓实体返回键后退的网页js实现
提供两种解决方法吧,都是网上来的,侵权删,毕竟我等只是搞后端的-- 第一种方法: // 阻止安卓实体键后退 // 页面载入时使用pushState插入一条历史记录 history.pushState( ...
- laravel中数据库在哪个文件中配置
我们使用 mysql 数据库,修改 .env: DB_HOST=localhost DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= 在mysql中创 ...
- Apache日志分析_shell命令行
说明: 1.我的日志预先设定好按日生成文件:"CustomLog "|/opt/apache/bin/rotatelogs /opt/apache/logs/www.website ...
- Python3 的数据类型
Python3 的数据类型 整形,浮点型,布尔类型 类型转换 int() 整形 采用截断的方式即向下取整,比如 a=5.5 int (a) 返回值为5 怎样才能使int()按照"四舍五入&q ...
- dedecms====phpcms 区别==[工作]
{template "content","header"}{dede:include filename="head.htm"/} ----- ...
- BIGIP-LTM中的NAT和SNAT
http://250688049.blog.51cto.com/643101/1095880 一.NAT(Network Address Translation)网络地址转换1.NAT简介 NAT ...