ATM Mechine
Total Submission(s): 676 Accepted Submission(s): 308
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.
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
Author
ZSTU
Source
2016 Multi-University Training Contest 5
题目大意:
Alice有一存折,他忘记有多少钱了,只记得里面数额不超过K
他想要用尽量少的次数取完里面的钱。已知当他取钱数超过拥有的钱的数目,police蜀黍回来警告他,警告超过W次他就会被赶走。
问Alice取完所有钱所需要的次数的期望。
取完不单单是指他存折里没钱了,Alice需要得到确定的信息告诉他取完了。
譬如Alice知道存折里最多1元,取一次就取完了。
或者当取1元的时候被警告了,也表示他存折里没钱了。
考虑
dpij
表示当前Alice直到存折里钱数上限为i,还可被警告j次,取完钱的期望。
这样
E(i,j)=minik=1i−k+1i+1∗E(i−k,j)+ki+1∗E(k−1,j−1)+1
边界
dp0j=0
(Alice知道自己最多0元钱)
dpi0=INF
(已经被police撵走了)
转移即可。
不过这样是
O(WK2)
复杂度,会超时。
考虑最坏情况 K == 2000 实际为0元。此时二分取钱最佳,被警告最多
logk2
次。也就是
W=min(W,logk2)
就好
因为输入对dp数组没有影响,可以预处理出来 直接查询。
代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)
using namespace std;
const double INF = 0x3f3f3f3f;
const int msz = 12;
const int mod = 1e9+7;
const double eps = 1e-8;
double dp[2333][13];
void init()
{
double mx;
for(int i = 0; i <= 2000; ++i)
{
for(int j = 0; j <= msz; ++j)
{
dp[i][j] = INF;
if(!i) dp[i][j] = 0;
else if(j)
{
for(int k = 1; k <= i; ++k)
{
dp[i][j] = min(dp[i][j],(i+1-k)*1.0/(i+1)*dp[i-k][j]+k*1.0/(i+1)*dp[k-1][j-1]);
}
dp[i][j] += 1;
}
}
}
}
int main()
{
//fread("");
//fwrite("");
init();
int k,w;
while(~scanf("%d%d",&k,&w))
{
w = min(w,msz);
printf("%.6f\n",dp[k][w]);
}
return 0;
}