hdu5781ATM Mechine+数学期望

博客介绍了一个关于ATM取款的算法问题,Alice忘记了自己的存款数额,只知道在0到K之间,每次尝试取钱,若余额不足则收到警告,超过W次警告会被警察带走。文章探讨了在不被警察带走的情况下,Alice如何采取最优策略确定存款数额所需的期望次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Author
ZSTU

Source
2016 Multi-University Training Contest 5

题目意思:Alice忘记了自己银行里存了多少钱,只记得在[0,k]之间。每次取钱如果余额足够就出钱,否则警告一次,警告超过w次就会把你抓起来,在不想被警察抓起来的前提下,Alice采取最优策略,求期望取钱多少次能知道自己存了多少钱。
官方给出的题解
这里写图片描述

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const double inf=1e12;
double f[2016][15];  //最多的钱,剩余可用的次数

double bank(int m,int w){
    if(m==0) return 0;
    if(w==0) return inf;
    if(f[m][w]>0) return f[m][w];

    double ans=inf;
    for(int i=1;i<=m;i++){  //枚举实际的钱数
        ans=min(ans,bank(i-1,w-1)*i/(m+1)+bank(m-i,w)*(m-i+1)/(m+1)+1);
        //为什么加1.。考虑的很久,菊苣已解释貌似就知道了。
        //试了一次之后,产生了两种步数期望,还要加上我试的这一次。
        //orz巨啊。。。
    }
    return f[m][w]=ans;
}

int main(){
    int k,w;
    fill(f[0],f[0]+2016 * 15,0.0);
    while(scanf("%d %d",&k,&w)!=EOF){
        w=min(w,15);//为什么15呢?因为二分查找2^15>2000
        printf("%.6f\n",bank(k,w));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值