hdu 5781 ATM Mechine

来源:互联网 发布:mac bsd 编辑:程序博客网 时间:2024/05/27 08:13
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

现场,没写出来。后来看题解补的;

因为当时没太理解题意;

题意:

爱丽丝去取钱,她忘了他具体有多少钱,但是知道最多有多少K;

他可以尝试着去取钱,如果取的钱小于等于他存的钱数,就取成功,否则被警告一次。

当他被警告超过W次后,她将会被警察带走。

问她不被警察带走且能取完钱的期望。

当然当取1元取不出时,说明钱全部取出;

题解:

E(i,j):存款的范围是[0,i],还可以被警告j次的期望值。

E(i,j) = max_{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(K^2W)O(K2W)的。 假如Alice使用的是二分策略,那么在最坏情况下至多被警告\left \lceil log_{2}{K} \right \rceillog2K 次。 于是W:=min(W,15)就可以了。

然后clar有人问y是不是要整数。由于存款是整数,你取小数的钱没有任何意义啊。

就是算出来所有值可能;

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <stack>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define LL long long#define N 200005#define mod 1000000007const double INF=999999999;double dp[2010][20];double solve(int k,int w){    if(k==0)        return 0;    if(w==0)        return INF;    if(dp[k][w]>0)        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 k,w;int main(){   while(~scanf("%d%d",&k,&w))   {       w=min(w,11);       printf("%0.6lf\n",solve(k,w));   }   return 0;}



0 0
原创粉丝点击