HDU 5781 ATM Mechine (概率dp)(求最优策略期望)

ATM Mechine

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 559    Accepted Submission(s): 245

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
Author
ZSTU
Source
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5792 5790 5789 5788 5787 

题意:有个人去ATM机里取钱,但是他不知道卡里有多少钱,而且这台ATM机不提供查询余额的功能,他只知道钱的上限 K,  每次他都要取一定的钱 y , 如果他的存款大于等于y, 他就得到 y钱 , 如果取的钱超过了余额,他就会被警告  1  次,求在最优策略下,取的钱上限是 K,警告次数不超过 W 。问你取钱次数期望最小是多少。

题解: E(i,j) :存款的范围是  [0,i]     , 还可以被警告 j次的期望值假如Alice使用的是二分策略,那么在最坏情况下至多被警告 log2K  次于是 W=min(W,15)  就可以了
E(i,j)   =   Minik=1ik+1i+1E(ik,j)+ki+1E(k1,j1)+1     
这样时间复杂度是 O(K2W) 。 然后有人问 y是不是要整数。由于存款是整数,你取小数的钱没有任何意义啊。

AC代码:
#include<bits/stdc++.h>
using namespace std;
const double INF = 1e12;
double dp[2010][20];
double solve(int k, int w)
{
    if(k == 0)
        return dp[k][w] = 0;
    if(w == 0)
        return INF;
    if(dp[k][w] > 0 )
        return dp[k][w];
    dp[k][w] = INF;
    for(int i=1; i<=k; i++)
        dp[k][w] = min(dp[k][w],(double)(k-i+1)/(k+1)*solve(k-i,w)+(double)i/(k+1)*solve(i-1,w-1)+1);
    return dp[k][w];
}
int main()
{
    int k, w;
    while(~scanf("%d%d",&k,&w))
    {
        w = min(w,15);
        printf("%.6lf\n",solve(k,w));
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值