HDU 5781 ATM Mechine(概率DP)

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.
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


题意:一个人有最多K元钱(可能有0-K元),当他当前取钱数少于他所拥有的钱数就会警告,警告超过W次(不包括W)后就会被警察带走。他会用最聪明的方式取钱,求他取出所有钱并不被警察带走所用次数的期望。


思路:dp[i][j]表示取款范围是[0,i],剩余被警告次数为j。

对于i,枚举k=0...i,k表示当前他实际上还剩k元存款。

对于每个k期望是:(i-k+1)/(i+1) * (dp[i-k][j] + 1) + k/(i+1) * (dp[k-1][j-1] + 1)。

dp[i][j]取结果最小的那次K即可。

表示i-k+1种情况下是顺利取出钱,k中情况下被警告,比前一状态多操作了一次,所以要+1。

总的复杂度是O(K*K*W)   由于用最好的取法,即二分,所以在log2(W)次内一定存在最优的期望,W=min(W,12)即可。

对于每个K,W都求一次会超时,需要先预处理出所有K=1~2000,W=1~12的情况。


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <iostream>
#include <assert.h>
#define INF 0x3f3f3f3f
using namespace std;

double dp[2005][25];

void init()
{
    for(int i=0;i<=2000;i++)
    {
        for(int j=0;j<=20;j++)
        {
            dp[i][j]=9999999999999.0;
        }
    }
    int w=15;
    for(int j=0;j<=w;j++)
        dp[0][j]=0;
    for(int i=1;i<=2000;i++)
    {
        for(int j=1;j<=w;j++)
        {
            for(int k=1;k<=i;k++)
            {
                dp[i][j]=min(dp[i][j],1.0*(i-k+1)/(i+1)*dp[i-k][j]+1.0*k/(i+1)*dp[k-1][j-1]+1.0);
            }
            //cout<<i<<"~"<<j<<"~"<<dp[i][j]<<endl;
        }
    }
}

int main()
{
    int k, w;
    init();
    while (scanf("%d%d",&k,&w)!=EOF)
    {
        w=min(15,w);
        printf("%.6lf\n",dp[k][w]);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值