hdu 5781 ATM Mechine(2016 Multi-University Training Contest 5——DP)

探讨在未知确切存款金额的情况下,如何在有限警告次数内,采用最优策略从ATM机取出所有存款并计算取款期望次数。

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5781

ATM Mechine

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


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
 

题目大意:

你只知道在银行中0-k之间这么多的存款,然后你要把银行里的钱全部都取出来。

但是在取钱过程中,如果你取的钱超过了你在银行存的钱,那么你会被警告。

警告次数不可以超过w次,超过w就会被警察抓走。

问采用最优策略之后,取完所有钱的期望是多少?


解题思路:

dp[i][j]表示:存款数为[0,i],还可以被警告j次。dp[i][j]表示最少要取多少次。

推导动态转移方程:

1、要是我这次取了钱k,但是没警告的话,就为dp[i-k][j]。

2、取钱警告了,方程为dp[k-1][j-1]。

这样方程为:dp[i][j]=min(dp[i][j],dp[i-k][j]+dp[k-1][j-1]+i+1);


详见代码。

#include <iostream>
#include <cstdio>

using namespace std;

#define INF 210000000
#define ll long long
ll dp[2010][21];

int main()
{
    for (int i=0;i<=2000;i++)
    {
        for (int j=0;j<=15;j++)
        {
            dp[i][j]=INF;
        }
    }
    for (int j=0;j<=15;j++)
    {
        dp[0][j]=0;
    }
    for (int i=1;i<=2000;i++)
    {
        for (int j=1;j<=15;j++)
        {
            for (int k=1;k<=i;k++)
            {
                dp[i][j]=min(dp[i][j],dp[i-k][j]+dp[k-1][j-1]+i+1);
            }
        }
    }
    int k,w;
    while (~scanf("%d%d",&k,&w))
    {
        if (w>15)
            w=15;
        ll a=dp[k][w];
        double ans=a*1.0/(k+1);
        printf ("%.6lf\n",ans);
    }
    return 0;
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值