Hdu-5781 ATM Mechine(DP)

来源:互联网 发布:tcl上网络电视要广告 编辑:程序博客网 时间:2024/05/17 05:11
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题意:你的银行账户中有[0,k]块钱,但是你不知道有多少,你每次可以取任意数量的钱,但是如果余额不足机器会报错,机器最多报W次错,然后再报错你就会被JC叔叔叫去喝茶,现在要求你把所有钱都取出来,问你取钱次数的期望。分析:E(i,j):存款的范围是[0,i],还可以被警告j次的期望值。

E(i,j) = maxk=1ii−k+1i+1∗E(i−k,j)+ki+1∗E(k−1,j−1)+1max_{k=1}^{i}{\frac{i-k+1}{i+1} * E(i-k,j)+\frac{k}{i+1}*E(k-1,j-1)+1}maxk=1ii+1ik+1E(ik,j)+i+1kE(k1,j1)+1这样时间复杂度是O(K2W)O(K^2W)O(K2W)的。假如Alice使用的是二分策略,那么在最坏情况下至多被警告⌈log2K⌉\left \lceil log_{2}{K} \right \rceillog2K 次。于是W:=min(W,15)就可以了。

#include<iostream>#include<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<cstring>#include<stack>#include<cmath>#include<queue>#define INF 0x3f3f3f3f#define eps 1e-9#define got(x) (1ll<<x)using namespace std;int k,w,f[2005][20]; int Find(int i,int j){if(i == 0) return 0;if(j == 1) return i*(i+1)/2+i;if(f[i][j] <= 4000000) return f[i][j];for(int k = 1;k <= i/2 + 1;k++) f[i][j] = min(Find(k-1,j-1)+Find(i-k,j)+i+1,f[i][j]);return f[i][j];}int main(){while(~scanf("%d%d",&k,&w)){memset(f,INF,sizeof(f));w = min(w,(int)log2(k)+1);printf("%.6lf\n",Find(k,w)/(k+1.0));}}


0 0