首页 > 代码库 > poj 2151 Check the difficulty of problems(线段树+概率)

poj 2151 Check the difficulty of problems(线段树+概率)

Check the difficulty of problems
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4465 Accepted: 1966

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: 
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? 

Input

The input consists of several test cases. The first line of each test case contains three integers M (0 < M <= 30), T (1 < T <= 1000) and N (0 < N <= M). Each of the following T lines contains M floating-point numbers in the range of [0,1]. In these T lines, the j-th number in the i-th line is just Pij. A test case of M = T = N = 0 indicates the end of input, and should not be processed.

Output

For each test case, please output the answer in a separate line. The result should be rounded to three digits after the decimal point.

Sample Input

2 2 2
0.9 0.9
1 0.9
0 0 0

Sample Output

0.972
每个节点记录这个区间每个队赢的概率。
#include <iostream>
#include <cstdio>
using namespace std;

const int maxn = 200;
const double eps = 0.0001;
struct tree{
    int l , r;
    double win[maxn];
}a[4*maxn];
double P[maxn][maxn];
int N[10] = {1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512};
int n;

void initial(){
    for(int i = 0; i < 4*maxn; i++){
        for(int j = 0; j < maxn; j++){
            a[i].win[j] = 0.0;
        }
    }
}

void build(int l , int r , int k){
    a[k].l = l;
    a[k].r = r;
    if(l == r){
        a[k].win[l] = 1.0;
    }else{
        int mid = (l+r)/2;
        build(l , mid , 2*k);
        build(mid+1 , r , 2*k+1);
        int i = l;
        while(i <= mid){
            for(int j = mid+1; j <= r; j++){
                a[k].win[i] += a[2*k].win[i]*a[2*k+1].win[j]*P[i][j];
            }
            i++;
        }
        while(i <= r){
            for(int j = l; j <= mid; j++){
                a[k].win[i] += a[2*k].win[j]*a[2*k+1].win[i]*P[i][j];
            }
            i++;
        }
    }
}

void readcase(){
    for(int i = 0; i < N[n]; i++){
        for(int j = 0; j < N[n]; j++){
            scanf("%lf" , &P[i][j]);
        }
    }
}

void computing(){
    build(0 , N[n]-1 , 1);
    int ans = 0;
    for(int i = 1; i < N[n]; i++){
        if(a[1].win[i] - a[1].win[ans] > eps){
            ans = i;
        }
    }
    printf("%d\n" , ans+1);
}

int main(){
    while(scanf("%d" , &n) && n != -1){
        initial();
        readcase();
        computing();
    }
    return 0;
}