zoukankan      html  css  js  c++  java
  • ATM Mechine 概率DP

    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.

    InputThe input contains multiple test cases. 
    Each test case contains two numbers K and W. 
    1K,W20001≤K,W≤2000OutputFor 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

    求期望逆推,求概率顺推
    分情况讨论,如果取钱成功。。如果取钱不成功。。。
    注意钱在给定的范围内是等可能分布的
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<memory>
    #include<bitset>
    #include<string>
    #include<functional>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    
    #define MAXN 2009 
    
    #define INF 0x3f3f3f3f
    
    /*
    概率DP 分为猜中和猜不中两部分计算
    dp[i][j] 目前已知金钱范围为0-i 
        当前还有j次猜的机会
    dp[i][j] =min( k从1到i 
    1 + 取钱成功:(i-k+1)/(i) * dp[i-k][j] + k/(i)*dp[k][j - 1])
    dp[1][x] = 1
    dp[x][0] = INF
    */
    double dp[MAXN][15];
    double E(int v, int k)
    {
        if (v == 0)
            return dp[v][k] = 0;
        else if (k == 0)
            return INF;
        else if (dp[v][k] > 0)
            return dp[v][k];
        else
        {
            dp[v][k] = INF;
            for (int i = 1; i <= v; i++)
            {
                dp[v][k] = min(dp[v][k], (double)( v - i +1)/( v + 1) * E(v - i, k) + (double)(i) / ( v + 1) *E(i - 1, k - 1) + 1.0);
            }
            return dp[v][k];
        }
    }
    int main()
    {
        int k, w;
        while (~scanf("%d%d", &k, &w))
        {
        //    memset(dp, 0, sizeof(dp));
            w = min(w, 15);
            printf("%.6lf
    ", E(k, w));
        }
    }


  • 相关阅读:
    常见的http状态码
    浅谈!DOCTYPE声明的作用?严格模式与混杂模式的区别?
    异步控制---实现函数asyncAll,在执行完传入数组中func1,func2,func3异步函数后,输出“end”
    Ecmascript 6新特性
    关于数组去重的几种方法-------javascript描述
    关于字符串的一些操作
    写一个将字符串转成驼峰命名的方法
    js作用域之常见笔试题,运行结果题
    CSS3实现图片黑白滤镜居中,hover缩放遮罩的效果
    远程桌面与本地桌面实现文件传输
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7395509.html
Copyright © 2011-2022 走看看