zoukankan      html  css  js  c++  java
  • CF518D Ilya and Escalator(期望DP)题解

    算法

    期望DP入坑题

    思路

    (f_{i,j})为第(i)时刻,电梯上正好有(j)人的概率,于是有:

    [left{egin{array}{}{f_{i + 1,j + 1}=f_{i,j} imes p}\{f_{i+1,j}=f_{i,j} imes (1-p)} end{array} ight. ]

    采用刷表法进行DP。边界条件:(f_{0,0}=1)

    注意:

    • 每次要额外处理(f_{i,n}),因为这一情况下不能再有人上电梯了;
    • 这样DP完以后只求出了概率,最后(Ans = sum^{n}_{i=1}{f_{t,i} imes i})
    • 由于使用了刷表法,所以时间那一维只需要循环(t-1)次!

    参考代码

    /*
     * @Author: When_C 
     * @Date: 2020-11-17 19:19:48 
     * @Last Modified by: When_C
     * @Last Modified time: 2020-11-17 19:36:01
     */
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 2010;
    
    int n,t;
    double p,f[maxn][maxn],Ans;
    
    int main(){
        scanf("%d%lf%d", &n, &p, &t);
        f[0][0] = 1;
        for(int i = 0; i < t; ++ i){
            f[i + 1][n] += f[i][n];
            for(int j = 0; j < n; ++ j){
                f[i + 1][j + 1] += f[i][j] * p;
                f[i + 1][j] += f[i][j] * (1 - p);
            }
        }
        for(int i = 1; i <= n; ++ i) Ans += f[t][i] * i;
        printf("%lf
    ", Ans);
        return 0;
    }
    
  • 相关阅读:
    Codeforces 1515 H. Phoenix and Bits 题解
    Codeforces 1511 F. Chainword 题解
    Codeforces 1369F. BareLee 题解
    莫队学习笔记
    圆方树学习笔记
    虚树学习笔记
    NOIP2020退役记
    CSP-S2 2021 场外
    我。
    WC2021 游记
  • 原文地址:https://www.cnblogs.com/whenc/p/13996095.html
Copyright © 2011-2022 走看看