ATM Mechine
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5781
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.
1≤K,W≤2000
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
Hint
题意
你在银行里面存了不超过k元的钱,然后你可以取钱。
如果你取的钱超过了你在银行存的钱,那么你会被警告。
你最多被警告w次,问你采用最优策略之后,期望取完所有钱的次数是多少
题解:
比较老的题了,原题是一个扔蛋的题。
E(i,j):存款的范围是[0,i],还可以被警告j次的期望值。
E(i,j) = \(min_{k=1}^{i}{\frac{i-k+1}{i+1} * E(i-k,j)+\frac{k}{i+1}*E(k-1,j-1)+1}\)这样时间复杂度是O(K^2W)的。 假如Alice使用的是二分策略,那么在最坏情况下至多被警告\(\left \lceil log_{2}{K} \right \rceil\) 次。 于是W:=min(W,15)就可以了。
代码
#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
#define lowbit(x) ((x)&(-x))
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 2e3 + 15;
int K,W;
double dp[maxn][maxn];
double dfs( int x , int y ){
if( dp[x][y] > -0.5 ) return dp[x][y];
double & ans = dp[x][y] = 1e9;
if( x == 0 ) return ans = 0;
if( y == 0 ) return ans = 1e9;
double px = 1./(double)(x + 1);
for(int k = 1 ; k <= x ; ++ k) ans = min( ans , (x - k + 1) * px * dfs( x - k , y ) + k * px * dfs( k - 1 , y - 1 ) + 1 );
return ans;
}
int main(int argc,char *argv[]){
for(int i = 0 ; i < maxn ; ++ i) for(int j = 0 ; j < maxn ; ++ j) dp[i][j] = -1;
while(~sf("%d%d",&K,&W)){
W = min( W , 15 );
pf("%.6lf\n" , dfs( K ,W )) ;
}
return 0;
}