# 概率DP_____ATM Mechine( hdu 5781 2016多校第五场)

370人阅读 评论(0)

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

dp[i][j] = min(dp[i][j] , dp[i-k][j]+dp[k-1][j-1]+1)然而时间复杂度为O(K^2M)但是仔细观察我们可以知道。爱丽丝是聪明的也就是采用二分的方式是出现警告最多的方式。也就是LogK <= 15. 所以最多只会出现15次警告。那么复杂度就降下来了。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
double dp[2010][16];

double dfs(int w,int c)
{
if(dp[w][c] != -1) return dp[w][c];
if(w == 0) return 0;
if( c == 0) return 1e9;
double ans = 1e9;
double k = 1.0/(1.0+w);
for(int i = 1 ; i <= w ; i ++ )
ans = min(ans,i*k*dfs(i-1,c-1)+(w+1-i)*k*dfs(w-i,c)+1);
return dp[w][c]=ans;
}
int main()
{

int i,j,k,w;
for (i=0;i<2010;i++)
for (j=0;j<16;j++) dp[i][j]=-1;
while (scanf("%d%d", &k, &w)!=EOF) {
w=min(w,15);printf("%.6f\n", dfs(k,w));
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：253601次
• 积分：4545
• 等级：
• 排名：第6479名
• 原创：226篇
• 转载：7篇
• 译文：0篇
• 评论：244条
阅读排行
评论排行
最新评论