hdu-5781 ATM Mechine(dp+概率期望)

题目链接:

ATM Mechine

Time Limit: 6000/3000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)


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
 
题意:

问等概率为[0,k]中的一个,最多被警告w次,问取钱次数的最小期望是多少;
 
思路:
 
题解讲的很清楚了,就是dp啦,二分可知警告次数不超过15次,每次选一个数取,能取出来和不能取出来两种情况,加在一起就是题解的式子了:
dp[i][j]=min( dp[i-k][j]*(i-k+1)/(i+1)+dp[k-1][j-1]*k/(i+1)+1 )
边界就是dp[0][i]=0;其他的要正无穷;
 
AC代码:
 
/************************************************
┆  ┏┓   ┏┓ ┆   
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆ 
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆      
************************************************ */ 
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+10;
const int maxn=2e3+14;
const double eps=1e-8;

double dp[maxn][17];

inline void Init()
{
    For(i,1,maxn-5)For(j,0,16)dp[i][j]=inf;
    For(j,1,16)dp[0][j]=0;
    For(i,1,maxn-5)
    {
        For(j,1,16)
        {
            For(x,1,i)
            {
                dp[i][j]=min(dp[i][j],dp[i-x][j]*(i-x+1)/(i+1)+dp[x-1][j-1]*x/(i+1)+1);
            }
        }
    }

}
int main()
{      
        Init();
        int k,w;
        while(cin>>k>>w)
        {
            printf("%.6lf
",dp[k][min(w,15)]);
        }
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5731986.html