POJ 3071 Football

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 are 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 are eliminated. After n rounds, only one team remains undefeated; this team is declared the winner.

Given a matrix P = [pij] such that pij is the probability that team i will beat team j in a match determine which team is most likely to win the tournament.

Input

The input test file will contain multiple test cases. Each test case will begin with a single line containing n (1 ≤ n ≤ 7). The next 2n lines each contain 2n values; here, the jth value on the ith line represents pij. The matrix P will 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 that you 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 is guaranteed that the difference in win probability for the top two teams will be at least 0.01.

Sample Input

2
0.0 0.1 0.2 0.3
0.9 0.0 0.4 0.5
0.8 0.6 0.0 0.6
0.7 0.5 0.4 0.0
-1

Sample Output

2

有2^n个球队比赛,一开始按次序1、2比,3、4比。。。。。2^n-1、2^n比。输的直接淘汰,赢者进入下一轮,一个矩阵中p[i][j]表示i打败j的概率。最后最可能哪个队夺冠?


概率dp,因为是两两一起比赛,此淘汰赛次序可当成一个完全二叉树,每轮都存下此队能走到这一轮的概率。dfs下去写法与线段树类似。
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string>
 4 #include<algorithm>
 5 #include<string.h>
 6 #include<math.h>
 7 using namespace std;
 8 double p[222][222];
 9 double dp[222][10];
10 int  N;
11 void dfs(int l, int r, int temp)//temp is the number of rounds||可以当成二叉树的层数
12 {
13     if (l == r)
14         return;
15     int mid = l + r >> 1;
16     dfs(l, mid, temp + 1);//向下递归
17     dfs(mid + 1, r, temp + 1);
18     int i, j;
19     for (i = l; i <= r; i++)
20     {
21         dp[i][temp] += (dp[i][temp] == 0);//如果当前点概率是0,则改为1,为后面相乘做准备
22         dp[i][temp - 1] = 0;//把走到下一轮的数组清空,准备累加
23     }
24     for (i = l; i <= mid; i++)
25     {
26         for (j = mid + 1; j <= r; j++)
27         {
28             dp[i][temp - 1] += dp[i][temp] * p[i][j] * dp[j][temp];//新的答案往temp-1放,最终答案是temp=0
29             dp[j][temp - 1] += dp[j][temp] * p[j][i] * dp[i][temp];//当前i走到这一轮的概率*j走到这一轮的概率*i打败j的概率
30         }//然后i与每个比赛的都加起来,所以是累加
31     }
32 }
33 int main()
34 {
35     int n;
36     while (scanf("%d", &n) != EOF)
37     {
38         if (n == -1)
39             break;
40         memset(p, 0, sizeof(p));
41         memset(dp, 0, sizeof(dp));
42         N = 1 << n;
43         int i, j;
44         for (i = 1; i <= N; i++)
45         {
46             for (j = 1; j <= N; j++)
47             {
48                 scanf("%lf", &p[i][j]);
49             }
50         }
51         dfs(1, N, 1);
52         int ans = 0;
53         double ma = 0;
54         for (i = 1; i <= N; i++)
55         {
56             if (dp[i][0]>ma)
57             {
58                 ma = dp[i][0];
59                 ans = i;
60             }
61         }//概率最大的那队获胜
62         printf("%d\n", ans);
63     }
64 }

				
时间: 2024-10-09 19:21:02

POJ 3071 Football的相关文章

POJ 3071 Football(简单 概率DP)

Football 原文链接:http://blog.csdn.net/xuechelingxiao/article/details/38520105 大意:2^n 个球队进行单场淘汰赛,每两只球队之间比赛会有胜负的概率,问最后谁夺冠的概率最大. 思路:简单的概率DP问题,主要是怎么处理哪两个球队比赛的问题. DP方程为 dp[i][j] = ∑(dp[i-1][j]*dp[i-1][k]*p[j][k]); //dp[i][j]表示第 i 轮的时候,第 j 支队伍赢的概率.. 对于其中位运算,可

POJ 3071 Football (动态规划-概率DP)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2768   Accepted: 1412 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

[ACM] POJ 3071 Football (概率DP)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2875   Accepted: 1462 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

poj 3071 Football (概率 dp)

链接:poj 3071 题意:有2^n个队,相邻的两两打淘汰赛,n轮后必定会决出冠军, 求最后哪个队夺冠的概率最大 分析:dp[i][j]表示第i轮的时候,第j去支队伍赢的概率 则dp[i][j]的前提就是i-1轮的时候,j是赢的,且第i轮赢了对方 接下来就是找到第i轮的时候,他的可能队手(难点) 所有对手的编号为1-2^n,通过二进制可以发现规律, 若第j队和对手k队在第i轮比赛,那么(j-1)和(k-1)二进制第i位刚好相反, 从i+1位开始所有高位是一样的,可以利用位运算判断是否为对手 状

【概率与期望】POJ 3071 Football

通道:http://poj.org/problem?id=3071 题意:有2^n个队伍,给出两两pk的胜率,求胜率最大的是哪个队.pk有限制,那就是相邻2个pk 思路:pk的方式其实就是在走一棵树,所以判断2者是否能够PK,那么判断最高几位就可以了,设dp[i][j]第i轮,j胜利的概率,则dp[i][j] = ∑dp[i-1][j]*dp[i-1][k]*p[j][k] 代码;https://github.com/Mithril0rd/Rojo/blob/master/poj3071.cpp

POJ 3071 Football:概率dp

题目链接:http://poj.org/problem?id=3071 题意: 给定n,有2^n支队伍参加足球赛. 给你所有的p[i][j],表示队伍i打败队伍j的概率. 淘汰赛制.第一轮(1,2)两队比.(3,4)比.(5,6)比...共进行n轮比赛后产生冠军. 问你冠军最有可能是哪支队伍. 题解: 表示状态: dp[i][j] = probability to win 第i支队伍能够参加第j轮比赛的概率. 找出答案: i of max dp[i][n+1] n轮比赛后,冠军该参加第n+1轮比

poj 3071 Football (概率DP水题)

G - Football Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2n. In each round of the tournament, all teams

POJ 3071 Football 【概率DP】

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 sti

poj 3071 Football DP

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3297   Accepted: 1687 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