POJ 3071 Football "Probability dp"

Source: Internet
Author: User

Football

Football
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3734 Accepted: 1908

Description

Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, ..., 2n. In each round of the tournament, all teams still in the tournament is placed in a list in order of increasing index. Then, the first team in the list plays the second team, the third team plays the fourth team, etc. The winners of these matches advance to the next round, and the losers is eliminated. After n rounds, only one team remains undefeated; This team is declared the winner.

Given a matrix P = [Pij] such that pij are the probability that team I 'll beat Team C7>j in a match determine which team are most likely to win the tournament.

Input

The input test file would contain multiple test cases. Each test case would begin with a single line containing n (1≤ n ≤7). The next 2n lines each contain 2n values; Here, the J-th value on the i-th line represents pij. The Matrix P would satisfy the constraints that pij = 1.0? Pji for all iJ, and pii = 0.0 for all i. The end-of-file is denoted by a single line containing the number? 1. Note that each of the matrix entries in this problem is given as a floating-point value. To avoid precision problems, make sure so use either the double data type instead of float .

Output

The output file should contain a single line for each test case indicating the number of the team most likely to win. To prevent floating-point precision issues, it's guaranteed, the difference in win probability for the top of the teams 'll is at least 0.01.

Sample Input

20.0 0.1 0.2 0.30.9 0.0 0.4 0.50.8 0.6 0.0 0.60.7 0.5 0.4 0.0-1

Sample Output

2

Hint

In the ' test case ' above, teams 1 and 2 and teams 3 and 4 play against each and the first round; The winners of each match then play to determine the winner of the tournament. The probability that Team 2 wins the tournament in this case is:

P (2 wins) = p(2 Beats 1)p(3 beats 4)p(2 beats 3) + p(2 Beats 1)p(4 beats 3)p(2 Beats 4)
= p+p +p p 24
= 0.9 · 0.6 · 0.4 + 0.9 · 0.4 · 0.5 = 0.396.

The next most likely team to win are Team 3, with a 0.372 probability of winning the tournament.


Test instructions: This question is for you a 2^n probability matrix P, P[i][j] is used to indicate the probability that team I will win the J team, the football match is based on the number from small to large to 22 matches, ask you most likely the last winning team number.

Analysis: Team 22 to play, until the winner, the process is required N-level competition "I can not think of any high-level vocabulary, on the layer, the language is not good, do not laugh ~", each layer has a number of two teams at the same time to play. Then according to the number 22 match, this time may wish to draw a figure out, we can find that this is a binary tree.

First, the set state Dp[i][j] indicates the probability of winning the team No. J in the level I race.

Then, set the state transfer equation: dp[i][j] = dp[i-1][j] *∑ (Dp[i-1][k]*p[j][k]), Ps:k∈ (I,J) In addition to (I-1,J) the other sub-node of all the leaf nodes below the number), really a bit of a mouthful, But as long as you draw a picture, it is very well understood.

Next, consider the boundary situation, obviously dp[0][j] = 0, or dp[1][j] = p[j][(j-1) ^1+1] "Another way of writing is dp[i][j] = p[j][j&1?j+1:j-1], with what kind of personal preference", j∈ ( <<n).

Finally, you only need to traverse all Dp[n][j], j∈ (1,1<<n), to find the maximum probability.

/** * MEMORY:444KB time:79ms * Author:__xiong 2015/7/27 * * #include <cmath> #include <cstdio> #include < string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm>using namespace Std;const int maxn = 7+1;const int maxm = (1&LT;&LT;MAXN); int n,m,ans;double PMAX,P[MAXM][MAXM],DP[MAXN][MAXM];    int main () {//freopen ("input.in", "R", stdin);        while (~SCANF ("%d", &n)) {if (N = =-1) break;        M = (1<<n); for (int i = 1; I <= m; i++) {for (int j = 1; J <= M; j + +) {scanf ("%lf",            &AMP;P[I][J]);        }} memset (Dp,0,sizeof (DP));                for (int i = 1, i <= N; i++) {for (int j = 1; J <= M; j + +) {if (i = = 1)                    {Dp[1][j] = p[j][j&1?j+1:j-1];                Continue    } for (int k = 1; k <= M; k++) {                int a = (j-1) >> (i-1), B = (k-1) >> (i-1);                    if (a&1) a--;                    else a++;                if (a = = b) Dp[i][j] + = dp[i-1][j]*dp[i-1][k]*p[j][k];        }}} PMax = 0;        Ans = 0;                for (int i = 1; I <= M; i++) {if (PMax < dp[n][i]) {ans = i;            PMax = Dp[n][i];    }} printf ("%d\n", ans); } return 0;}

In addition, hang up the code of the base god.

Pro-Test: memory:1080kb time:79ms#include<cstdio> #include <cstring> #include <algorithm>using namespace Std;const int MX = 5, #define for (i,x,y) for (int i=x;i<=y;i++) #define MEM (x, y) memset (x,y,sizeof (×)) #def Ine Lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define root 1,n,1int n;double a[mx][mx];d ouble dp[MX << 2]        [Mx];void Solve (int l, int r, int rt) {if (L = = r) {Dp[rt][l] = 1;    Return    } int m = (L + r) >> 1;    Solve (Lson);    Solve (Rson);  For (X, L, R) {if (x <= m) {for (I, M + 1, R) {dp[rt][x] + = Dp[rt << 1 |            1][i] * A[x][i];        } Dp[rt][x] *= dp[rt << 1][x];            } else {for (I, L, m) {dp[rt][x] + = Dp[rt << 1][i] * A[x][i];        } Dp[rt][x] *= dp[rt << 1 | 1][x];    }}}int Main () {//freopen ("input.in", "R", stdin);    int n; WhiLe (~scanf ("%d", &n), n >= 0) {Mem (DP, 0);        n = 1 << n;            For (i, 1, n) {for (J, 1, N) {scanf ("%lf", &a[i][j]);        }} solve (root);        Double Max = 0;        int ans;                For (i, 1, n) {if (Dp[1][i] > Max) {max = dp[1][i];            ans = i;    }} printf ("%d\n", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3071 Football "Probability dp"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.