HDU 5781 ATM Mechine(概率DP求期望)

来源:互联网 发布:hbuilder下载 mac 编辑:程序博客网 时间:2024/04/30 12:55

传送门

ATM Mechine

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


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


题目大意:
一个人去ATM机里取钱,但是他不知道卡里有多少钱,而且这个ATM机不提供查询余额的功能,他只知道钱的上限 K, 每次他都要取一定的钱 y,如果他的存款 >=y,他马上就得到 y 元钱, 如果取的钱超过了余额,他就会被警告 1 次,求在最优策略下,取的钱上限是 K,警告次数不超过 W 取钱次数期望最小是多少。

解题思路:
E(i,j) 存款的范围是 [0,i],还可以被警告j次的期望值。假如Alice使用的是二分策略,那么在最坏情况下至多被警告log2K 次。于是W=min(W,12) 就可以了。E(i,j) = Minik=1ik+1i+1E(ik,j)+ki+1E(k1,j1)+1
这样时间复杂度是O(K2W)的。
My Code

/**2016 - 08 - 03 上午Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。**/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <algorithm>#include <set>using namespace std;typedef long long LL;typedef unsigned long long ULL;const double INF = 1e12;const int MAXN = 2e3+5;const int MOD = 1e9+7;const double eps = 1e-7;double dp[MAXN][20];double Solve(int k, int w){    if(k == 0)        return dp[k][w] = 0;    if(w == 0)        return INF;    if(dp[k][w] > eps)        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,12);        printf("%.6lf\n",Solve(k,w));    }    return 0;}
0 1