Hdu-5781 ATM Mechine(DP)

185 篇文章 0 订阅
116 篇文章 0 订阅
Problem 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.
1K,W2000
 

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 题意:你的银行账户中有[0,k]块钱,但是你不知道有多少,你每次可以取任意数量的钱,但是如果余额不足机器会报错,机器最多报W次错,然后再报错你就会被JC叔叔叫去喝茶,现在要求你把所有钱都取出来,问你取钱次数的期望。 分析:E(i,j):存款的范围是[0,i],还可以被警告j次的期望值。

E(i,j) = maxk=1ii−k+1i+1∗E(i−k,j)+ki+1∗E(k−1,j−1)+1max_{k=1}^{i}{\frac{i-k+1}{i+1} * E(i-k,j)+\frac{k}{i+1}*E(k-1,j-1)+1}maxk=1ii+1ik+1E(ik,j)+i+1kE(k1,j1)+1这样时间复杂度是O(K2W)O(K^2W)O(K2W)的。假如Alice使用的是二分策略,那么在最坏情况下至多被警告⌈log2K⌉\left \lceil log_{2}{K} \right \rceillog2K 次。于是W:=min(W,15)就可以了。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define got(x) (1ll<<x)
using namespace std;
int k,w,f[2005][20]; 
int Find(int i,int j)
{
	if(i == 0) return 0;
	if(j == 1) return i*(i+1)/2+i;
	if(f[i][j] <= 4000000) return f[i][j];
	for(int k = 1;k <= i/2 + 1;k++) f[i][j] = min(Find(k-1,j-1)+Find(i-k,j)+i+1,f[i][j]);
	return f[i][j];
}
int main()
{
	while(~scanf("%d%d",&k,&w))
	{
		memset(f,INF,sizeof(f));
		w = min(w,(int)log2(k)+1);
		printf("%.6lf\n",Find(k,w)/(k+1.0));
	}
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值