POJ2151 Check the difficulty of problems 概率DP

Description

Organizing a programming contest is not an easy job. To avoid making the problems too difficult, the organizer usually expect the contest result satisfy the following two terms:
– All of the teams solve at least one problem.
– The champion (One of those teams that solve the most problems) solves at least a certain number of problems.
Now the organizer has studied out the contest problems, and through the result of preliminary contest, the organizer can estimate the probability that a certain team can successfully solve a certain problem.
Given the number of contest problems M, the number of teams T, and the number of problems N that the organizer expect the champion solve at least. We also assume that team i solves problem j with the probability Pij (1 <= i <= T, 1<= j <= M). Well, can you calculate the probability that all of the teams solve at least one problem, and at the same time the champion team solves at least N problems?

  • 题意
    M 道题, T 个队伍,给出所有队伍分别解出每个题的概率 pij ,求每个队至少解出1题且冠军队解出至少 N 题的概率。

  • 解析
    涉及到 至少 这个词,不妨使用一些前缀和技巧。
    假设 p1 是所有队都解出至多 M 题的概率, p2 是所有队解出至多 N1 题的概率,那么最后的答案就是 p1p2
    我们需要分别计算每一队的贡献,再把相应的答案乘起来从而计算 p1 p2
    f[i][j] 代表当前是前 i 道题,解出了一共 j 道题的概率。不难发现转移方程为

    f[i][j]=f[i1][j1]×p[i][j]+f[i1][j]×(1p[i][j])
    再利用 sum[i] 代表所有题中解出至多 i 道题的概率,其实就是前缀和。
    那么这个队对于 p1 的贡献就是 sum[M]sum[0] p2 的贡献是 sum[N1]sum[0]

  • 数组可以滚,你不滚也没关系

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <cctype>
#include <string>
using namespace std ;
const int maxn = 1005, maxm = 35 ;
int n, m, e ;
double p[maxm], f[2][maxm], sum[maxm] ;
int main() {
    int i, j, k ;
    while ( scanf ( "%d %d %d", &m, &n, &e ) != EOF ) {
        if ( !n ) break ;
        double p1 = 1.0, p2 = 1.0 ;
        for ( i = 1 ; i <= n ; i ++ ) {
            for ( j = 1 ; j <= m ; j ++ )
                scanf ( "%lf", &p[j] ) ;
            memset ( f, 0.0, sizeof f ) ;
            int u, v ;
            u = 0, v = 1 ;
            memset ( f, 0.0, sizeof f ) ;
            f[0][0] = 1.0 ;
            for ( j = 1 ; j <= m ; j ++, swap(u, v) )
                for ( k = 0 ; k <= j ; k ++ )
                    f[v][k] = f[u][k-1]*p[j]+f[u][k]*(1-p[j]) ;
            sum[0] = f[u][0] ;
            for ( j = 1 ; j <= m ; j ++ )
                sum[j] = sum[j-1]+f[u][j] ;
            p1 *= sum[m]-sum[0] ;
            p2 *= sum[e-1]-sum[0] ;
        }
        printf ( "%.3lf\n", p1-p2 ) ;
    }
    return 0 ;
}
点赞