hdu 5781 ATM Mechine dp
ATM Mechine
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5781
Description
Alice is going to take all her savings out of the ATM(Automatic Teller Machine). Alice forget how many deposit she has, and this strange ATM doesn't support query deposit. The only information Alice knows about her deposit is the upper bound is K RMB(that means Alice's deposit x is a random integer between 0 and K (inclusively)).
Every time Alice can try to take some money y out of the ATM. if her deposit is not small than y, ATM will give Alice y RMB immediately. But if her deposit is small than y, Alice will receive a warning from the ATM.
If Alice has been warning more then W times, she will be taken away by the police as a thief.
Alice hopes to operate as few times as possible.
As Alice is clever enough, she always take the best strategy.
Please calculate the expectation times that Alice takes all her savings out of the ATM and goes home, and not be taken away by the police.
Input
The input contains multiple test cases.
Each test case contains two numbers K and W.
1≤K,W≤2000
Output
For each test case output the answer, rounded to 6 decimal places.
Sample Input
1 1
4 2
20 3
Sample Output
1.000000
2.400000
4.523810
Hint
题意
你在银行里面存了不超过k元的钱,然后你可以取钱。
如果你取的钱超过了你在银行存的钱,那么你会被警告。
你最多被警告w次,问你采用最优策略之后,期望取完所有钱的次数是多少
题解:
比较老的题了,原题是一个扔蛋的题。
E(i,j):存款的范围是[0,i],还可以被警告j次的期望值。
E(i,j) = \(min_{k=1}^{i}{\frac{i-k+1}{i+1} * E(i-k,j)+\frac{k}{i+1}*E(k-1,j-1)+1}\)这样时间复杂度是O(K^2W)的。 假如Alice使用的是二分策略,那么在最坏情况下至多被警告\(\left \lceil log_{2}{K} \right \rceil\) 次。 于是W:=min(W,15)就可以了。
代码
#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
#define lowbit(x) ((x)&(-x))
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 2e3 + 15;
int K,W;
double dp[maxn][maxn];
double dfs( int x , int y ){
if( dp[x][y] > -0.5 ) return dp[x][y];
double & ans = dp[x][y] = 1e9;
if( x == 0 ) return ans = 0;
if( y == 0 ) return ans = 1e9;
double px = 1./(double)(x + 1);
for(int k = 1 ; k <= x ; ++ k) ans = min( ans , (x - k + 1) * px * dfs( x - k , y ) + k * px * dfs( k - 1 , y - 1 ) + 1 );
return ans;
}
int main(int argc,char *argv[]){
for(int i = 0 ; i < maxn ; ++ i) for(int j = 0 ; j < maxn ; ++ j) dp[i][j] = -1;
while(~sf("%d%d",&K,&W)){
W = min( W , 15 );
pf("%.6lf\n" , dfs( K ,W )) ;
}
return 0;
}
hdu 5781 ATM Mechine dp的更多相关文章
- HDU 5781 ATM Mechine 期望dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 ATM Mechine Time Limit: 6000/3000 MS (Java/Othe ...
- HDU 5781 ATM Mechine (概率DP)
ATM Mechine 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...
- 【动态规划】HDU 5781 ATM Mechine
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 题目大意: 一个人有[0,K]内随机的钱,每次可以随意取,但是不知道什么时候取完,取钱超过剩余 ...
- HDU 5781 ATM Mechine
题目大意:某个未知整数x等概率的分布在[0,k]中.每次你都可以从这个整数中减去一个任意整数y,如果x>=y,那么x=x-y,操作次数累计加1:否则,将会受到一次错误提示.当错误提示超过w次,将 ...
- hdu-5781 ATM Mechine(dp+概率期望)
题目链接: ATM Mechine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU5781 ATM Mechine(DP 期望)
应该是machine 和POJ3783 Balls类型相似. 现在上界为i元,猜错次数最多为j时,开始猜测为k元,有两种情况: 1 猜中:(i - k + 1) * dp[i - k][j] 2 猜不 ...
- 多校5 1001 HDU5781 ATM Mechine 记忆化搜索+概率
// 多校5 1001 HDU5781 ATM Mechine // http://acm.hdu.edu.cn/search.php?field=problem&key=2016+Multi ...
- 2016 Multi-University Training Contest 5 ATM Mechine
ATM Mechine 本文转自:http://blog.csdn.net/queuelovestack/article/details/52096337 题意: 这题的意思还是比较费解的 Alice ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
随机推荐
- [C]语法, 知识点总结(二. 结构体, 类别名, static, const)
结构体 定义: struct Student{ // 定义结构体Student, stu是创建的对象 char a[17]; // 结构体里面可以有多种不同类型的变量 ...
- Red Pen - 快速高效的获取设计项目的反馈
Red Pen 让设计师能够快速,高效的从你的同事和客户获取反馈.只需要简单的拖放图像到 Red Pen 主页,然后把生成的链接分享给你的同事或者客户.他们打开链接就能看到设计稿,并给予实时的反馈,所 ...
- springboot 整合 mongodb实现 批量更新数据
现需求:需要批量将1000个数据先查询在更新到mongodb(如果查询不到数据,则添加数据) 1:工具类BathUpdateOptions import org.springframework.dat ...
- 图的最短路径-----------Dijkstra算法详解(TjuOj2870_The Kth City)
做OJ需要用到搜索最短路径的题,于是整理了一下关于图的搜索算法: 图的搜索大致有三种比较常用的算法: 迪杰斯特拉算法(Dijkstra算法) 弗洛伊德算法(Floyd算法) SPFA算法 Dijkst ...
- windows下nodejs服务器的安装与配置
1下载安装 download from the link: https://nodejs.org/en/ windows下的安装直接运行exe,略过-- 注:由于用户权限的问题,最好将nodejs安装 ...
- mysql学习------权限机制
MySQL服务器通过MySQL权限表来控制用户对数据库的访问,MySQL权限表存放在mysql数据库里,由mysql_install_db脚本初始化.这些MySQL权限表分别user,db,table ...
- mysql主从复制跳过复制错误【转】
跳过复制错误 mysql因为binlog机制问题,有些时候会出现从库重放sql执行失败的情况,特别是旧的STATEMENT模式最容易出现这种情况(因为函数和存储过程等原因),这也是为什么强调使用mix ...
- linux系统下如何批量更改文件夹里面所有相同字符【转】
如网上源码下载下来我们需要调试,调试过程中需要修改里面相同文字,下面提供三种方法供参考 通过sed命令替换 sed -i "s/oldyunwei/newyunwei/g" gre ...
- mitmproxy实践
首先附上github地址:https://github.com/mitmproxy/mitmprox,上面的内容永远是最新的 作为一名测试穿戴设备相关app的工程师,与数据打交道是常事,那么,如果想要 ...
- shell脚本 ------ 输出带颜色的字体
shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数-e 格式如下: echo -e “\033[字背景颜色:文字颜色m字符串\033[0m” 例如: echo -e “\03 ...