BZOJ1492: [NOI2007]货币兑换Cash 【dp + CDQ分治】
1492: [NOI2007]货币兑换Cash
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 5391 Solved: 2181
[Submit][Status][Discuss]
Description
.png)
.png)
Input
Output
只有一个实数MaxProfit,表示第N天的操作结束时能够获得的最大的金钱数目。答案保留3位小数。
Sample Input
1 1 1
1 2 2
2 2 3
Sample Output
HINT
题解
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define fo(i,x,y) for (int i = (x); i <= (y); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1;char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = out * 10 + c - 48; c = getchar();}
return out * flag;
}
int N;
double S,A[maxn],B[maxn],R[maxn],f[maxn],ans;
int main()
{
N = read(); S = read();
REP(i,N) A[i] = read(),B[i] = read(),R[i] = read();
ans = S;
f[1] = S / (R[1] * A[1] + B[1]);
for (int i = 2; i <= N; i++){
for (int j = 1; j < i; j++)
ans = max(ans,f[j] * B[i] + R[j] * f[j] * A[i]);
f[i] = ans / (R[i] * A[i] + B[i]);
}
printf("%.3lf\n",f[N] * B[N] + R[N] * f[N] * A[N]);
return 0;
}
* f[k] / (R[k] * A[k] + B[k])
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define eps 1e-9
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define fo(i,x,y) for (int i = (x); i <= (y); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
int n,top,st[maxn];
double f[maxn];
struct node{double x,y,a,b,r,k;int id;}p[maxn],t[maxn];
inline bool operator < (const node& a,const node& b){return a.k > b.k;}
inline double slope(int u,int v){
if (!v) return -1e20;
if (fabs(p[u].x - p[v].x) < eps) return 1e20;
return (p[u].y - p[v].y) / (p[u].x - p[v].x);
}
void solve(int L,int R){
if (L == R){
f[L] = max(f[L],f[L - 1]);
p[L].y = f[L] / (p[L].a * p[L].r + p[L].b);
p[L].x = p[L].r * p[L].y;
return;
}
int mid = (L + R) >> 1,l1 = L,l2 = mid + 1,j = 1;
for (int i = L; i <= R; i++)
if (p[i].id <= mid) t[l1++] = p[i];
else t[l2++] = p[i];
for (int i = L; i <= R; i++) p[i] = t[i];
solve(L,mid);
top = 0;
for (int i = L; i <= mid; i++){
while (top > 1 && slope(st[top - 1],st[top]) < slope(st[top - 1],i) + eps) top--;
st[++top] = i;
}
st[++top] = 0;
for (int i = mid + 1; i <= R; i++){
while (j < top && slope(st[j],st[j + 1]) + eps > p[i].k) j++;
f[p[i].id] = max(f[p[i].id],p[st[j]].x * p[i].a + p[st[j]].y * p[i].b);
}
solve(mid + 1,R);
l1 = L; l2 = mid + 1;
for (int i = L; i <= R; i++){
if (((p[l1].x < p[l2].x || (fabs(p[l1].x - p[l2].x) < eps && p[l1].y < p[l2].y)) || l2 > R) && l1 <= mid) t[i] = p[l1++];
else t[i] = p[l2++];
}
for (int i = L; i <= R; i++) p[i] = t[i];
}
int main()
{
scanf("%d%lf",&n,&f[0]);
REP(i,n){
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 + 1,p + 1 + n);
solve(1,n);
printf("%.3lf\n",f[n]);
return 0;
}
BZOJ1492: [NOI2007]货币兑换Cash 【dp + CDQ分治】的更多相关文章
- bzoj1492 [NOI2007]货币兑换Cash【cdq分治】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1492 推荐博客:http://www.cnblogs.com/zig-zag/archive ...
- 1492: [NOI2007]货币兑换Cash【CDQ分治】
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 4166 Solved: 1736[Submit][Sta ...
- 【BZOJ】1492: [NOI2007]货币兑换Cash(cdq分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=1492 蒟蒻来学学cdq神算法啊.. 详见论文 陈丹琦<从<Cash>谈一类分治算法 ...
- 【BZOJ1492】货币兑换Cash(CDQ分治)
题意: 小 Y 最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A 纪 念券(以下简称 A 券)和 B 纪念券(以下简称 B 券).每个持有金券的顾客都有 一个自己的帐户.金券的数目可以是一 ...
- [BZOJ1492] [NOI2007] 货币兑换Cash(cdq分治+斜率优化)
[BZOJ1492] [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 ...
- [BZOJ1492][NOI2007]货币兑换Cash(斜率优化+CDQ分治)
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5838 Solved: 2345[Submit][Sta ...
- 【BZOJ-1492】货币兑换Cash DP + 斜率优化 + CDQ分治
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 3396 Solved: 1434[Submit][Sta ...
- [BZOJ1492] [NOI2007]货币兑换Cash 斜率优化+cdq/平衡树维护凸包
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5907 Solved: 2377[Submit][Sta ...
随机推荐
- unity面试题二
1.以下哪一个选项不属于Unity引擎所支持的视频格式文件(D) A.后缀为mov的文件 B.后缀为mpg的文件 C.后缀为avi的文件 D.后缀为swf的文件 2.Unity引擎使用的是左手坐标系还 ...
- http tcp udp
HTTP连接 1.HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用 ...
- MySQL日期函数、时间函数总结(MySQL 5.X)
一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...
- 【system.string】使用说明
对象:system.string 说明:提供一系列针对字符串类型的操作 目录: 方法 返回 说明 system.string.isBlank( string ) [True | False] 检测参 ...
- (转)CGMA - Organic World Building in UE4: week 6
原文:丢失,这篇是艺术家博客上发现的,小道整理笔记中,临时放于效果案例目录. In this week we focused on creating the grass and flora t ...
- python作业:三级菜单(第一周)
一.作业需求: 1. 运行程序输出第一级菜单 2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 3. 菜单数据保存在文件中 4. 让用户选择是否要退出 5. 有返回上一级菜单的功能 二.三级菜单 ...
- Python+Flask+Gunicorn 项目实战(一) 从零开始,写一个Markdown解析器 —— 初体验
(一)前言 在开始学习之前,你需要确保你对Python, JavaScript, HTML, Markdown语法有非常基础的了解.项目的源码你可以在 https://github.com/zhu-y ...
- mybatis 枚举类型使用
一.首先定义接口,提供获取数据库存取的值得方法,如下: public interface BaseEnum { int getCode(); } 二.定义mybatis的typeHandler扩展类, ...
- 2019-1-92.4G射频芯片培训资料
2019-1-92.4G射频芯片培训资料 培训 RF 小书匠 欢迎走进zozo的学习之旅. 2.4G芯片选型 2.4G芯片开发 Q&A 2.4G芯片选型 芯片类型 soc 防盗标签2.4G无 ...
- “Hello World!团队”Alpha发布—视频链接+文案+美工
视频链接:http://v.youku.com/v_show/id_XMzEyNjc2MTAyOA==.html?sharefrom=iphone&sharekey=5378037f8b710 ...