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

来源:互联网 发布:域名购买后要做什么 编辑:程序博客网 时间:2024/05/18 03:45

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 14 220 3
Sample Output
1.0000002.4000004.523810
Author
ZSTU
Source
2016 Multi-University Training Contest 5
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
原创粉丝点击