【HDU 5781】ATM Mechine(概率DP)

【HDU 5781】ATM Mechine(概率DP)

ATM Mechine

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

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.

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
2016 Multi-University Training Contest 5

题目大意:
Alice有一存折,他忘记有多少钱了,只记得里面数额不超过K
他想要用尽量少的次数取完里面的钱。已知当他取钱数超过拥有的钱的数目,police蜀黍回来警告他,警告超过W次他就会被赶走。
问Alice取完所有钱所需要的次数的期望。
取完不单单是指他存折里没钱了,Alice需要得到确定的信息告诉他取完了。
譬如Alice知道存折里最多1元,取一次就取完了。
或者当取1元的时候被警告了,也表示他存折里没钱了。

考虑 dpij 表示当前Alice直到存折里钱数上限为i,还可被警告j次,取完钱的期望。
这样 E(i,j)=minik=1ik+1i+1E(ik,j)+ki+1E(k1,j1)+1
边界 dp0j=0 (Alice知道自己最多0元钱)
dpi0=INF (已经被police撵走了)
转移即可。
不过这样是 O(WK2) 复杂度,会超时。
考虑最坏情况 K == 2000 实际为0元。此时二分取钱最佳,被警告最多 logk2 次。也就是 W=min(W,logk2) 就好

因为输入对dp数组没有影响,可以预处理出来 直接查询。

代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const double INF = 0x3f3f3f3f;
const int msz = 12;
const int mod = 1e9+7;
const double eps = 1e-8;

double dp[2333][13];

void init()
{

    double mx;

    for(int i = 0; i <= 2000; ++i)
    {
        for(int j = 0; j <= msz; ++j)
        {
            dp[i][j] = INF;
            if(!i) dp[i][j] = 0;
            else if(j)
            {
                for(int k = 1; k <= i; ++k)
                {
                    dp[i][j] = min(dp[i][j],(i+1-k)*1.0/(i+1)*dp[i-k][j]+k*1.0/(i+1)*dp[k-1][j-1]);
                }
                dp[i][j] += 1;
            }
        }
    }

}

int main()
{
    //fread("");
    //fwrite("");

    init();

    int k,w;

    while(~scanf("%d%d",&k,&w))
    {
        w = min(w,msz);
        printf("%.6f\n",dp[k][w]);
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值