zoukankan      html  css  js  c++  java
  • Pong’s Birds(概率 模拟)

    Pong’s Birds

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 98  解决: 24
    [提交][状态][讨论版]

    题目描述

    In order to train his birds, Pong is holding a competition for them. (He have birds, does he?) He have 2n birds,
    so he want to hold elimination series. Literally, if n = 2 and he has 4 birds identified as 1,2,3,4, he would first hold competition for 1 and 2, 3 and 4, the one fails would be eliminated and then he holds competition for the winners. 
    According to his long observation, for each pair of birds he knows the probability to win for each bird, and he want to know the probability of each bird to be the final winner.

    输入

    For the first row there is an integer T(T ≤ 100), which is the number of test cases.
    For each case , first row is an integer n(1 ≤ n ≤ 7), for the next 2n rows , each row has 2n real numbers as the probability to win for the i-th when competed to j-th bird. It is promised that for each i, j p[i][j] + p[j][i] = 1 and p[i][i] = 0;

    输出

    For each case you should output a line with 2n real numbers, which is the probability of i-th bird be the final winner. Rounded to 3 decimal places.
    Make sure to output a space character after each number, to prevent the Presentation Error.

    样例输入

    1
    1
    0 0.5
    0.5 0
    

    样例输出

    0.500 0.500
    

    提示


    2^n个鸟比赛,1和2,3和4...,然后每个赢的在和相邻的比,输的就被pass掉,然后求每个鸟成为winner的概率。

    其实就是个模拟的过程,不过这里真正处理起来还是有点技巧和规律的。


    这里有个奇偶互换的想法挺好的!!!


    #include <bits/stdc++.h>
    using namespace std;
     
    const int MAXN = (1 << 7) + 5;
    double p[MAXN][MAXN];
    double dp[8][MAXN];//dp[i][j]表示第i轮j队伍出线概率
     
    int main()
    {
        int t;
        int n;
        int n2;
        int i, j, k;
        int grp;
        for (j = 0; j < MAXN; ++j) {
            dp[0][j] = 1;
        }
        scanf("%d", &t);
        while (t--) {
            scanf("%d", &n);
            n2 = 1 << n;
            for (i = 0; i < n2; ++i) {
                for (j = 0; j < n2; ++j) {
                    scanf("%lf", &p[i][j]);
                }
            }
     
            for (i = 1; i <= n; ++i) {
                for (j = 0; j < n2; ++j) {
                    grp = j / (1 << (i - 1));
                    grp ^= 1;//奇偶互换
                    dp[i][j] = 0;
                    for (k = grp << (i - 1); k < ((grp + 1) << (i - 1)); ++k) {
                        dp[i][j] += dp[i - 1][j] * dp[i - 1][k] * p[j][k];
                    }
                }
            }
     
            for (i = 0; i < n2 - 1; ++i) {
                printf("%.3lf ", dp[n][i]);
            }
            printf("%.3lf
    ", dp[n][n2 - 1]);
        }
        return 0;
    }







  • 相关阅读:
    矩阵十题(7)
    线段树成段更新 poj 3468 A Simple Problem with Integers
    线段树单点更新 hdu 2795 Billboard
    线段树成段更新 poj 2528 Mayor's posters
    矩阵十题(10)
    矩阵十题(8)
    矩阵十题(5)
    矩阵十题(6)
    矩阵十题(9)
    矩阵十题(4)
  • 原文地址:https://www.cnblogs.com/zswbky/p/8454140.html
Copyright © 2011-2022 走看看