POJ2151正难则反

题目描述

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:
1. All of the teams solve at least one problem.
2. 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?

算法思路

  1. 这一题用了高中数学一个十分重要的思想–正难则反。
  2. 所有的队伍都必须至少通过1题,看起来十分麻烦,因为正面去求的话要使用组合数。但是,这个条件的逆命题就是存在一个队伍一题都没有通过,很显然这是非常好求的。
  3. 剩下的第二个条件,冠军队至少要通过n题。也就是至少有一队要通过n题。看起来,这又要运用组合数了。但是我们看到,这一题的反面就是没有1支队可以通过至少n题。这也是非常好解的。
  4. 减小算法的空间复杂度,我们只需要dp[i][j]是当前所求的队伍中前i题过j题的概率即可。最后累加我们的结果就可以得到最后的解。算法的时间复杂度为O( nmt )

代码


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

#define MAXN 35
#define MAXT 1005
#define INF 10e+20
#define ZERO 10e-6

double Arr[MAXT][MAXN];
double dp[MAXN][MAXN];
double noWin[MAXT];
int N,M,T;
double ans1,ans2;

void Solve()
{
    int i,j,k;
    double temp;
    ans2=1.0;

    for(i=0;i<T;i++){
        memset(dp,0,sizeof(dp));
        dp[1][0]=1-Arr[i][1];
        dp[1][1]=Arr[i][1];
        for(j=2;j<=M;j++){
            dp[j][0] = dp[j-1][0]*(1-Arr[i][j]);
            for(k=1;k<=j;k++)
                dp[j][k] = dp[j-1][k-1]*Arr[i][j] + dp[j-1][k]*(1-Arr[i][j]);
        }
        temp = 0;
        //add up to find the solution
        for(j=1;j<N;j++)
            temp += dp[M][j];
        ans2 *= temp;
    }
}

int main()
{
    freopen("input","r",stdin);
    int i,j;

    while(scanf("%d%d%d",&M,&T,&N)!=EOF){
        if(!T && !M && !N)break;
        ans1 = 1.0;
        for(i=0;i<T;i++){
            noWin[i] = 1.0;
            for(j=1;j<=M;j++){
                scanf("%lf",&Arr[i][j]);
                noWin[i] *= (1-Arr[i][j]);
            }
            //compute the probability of every team solve 
            //at least one problem
            ans1 *= (1-noWin[i]);
        }
        Solve();
        printf("%.3f\n",ans1-ans2);
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值