POJ 3071 Football
来源:互联网 发布:hello world程序员梗 编辑:程序博客网 时间:2024/06/08 01:29
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. Aftern rounds, only one team remains undefeated; this team is declared the winner.
Given a matrix P = [pij] such thatpij 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 containingn (1 ≤ n ≤ 7). The next 2n lines each contain 2n values; here, thejth value on the ith line represents pij. The matrixP will satisfy the constraints that pij = 1.0 − pji for all i ≠ j, and pii = 0.0 for alli. 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 thedouble
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
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
有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=029 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 }
原博客 http://www.cnblogs.com/jinmingyi/
- poj 3071 Football
- poj 3071 football
- POJ 3071 Football
- POJ-3071-Football
- poj 3071 Football
- poj 3071 Football
- POJ 3071 Football
- POJ 3071 Football
- POJ 3071 Football
- POJ 3071 Football
- poj 3071 Football
- POJ 3071 Football
- poj 3071 Football DP
- POJ 3071:Football
- POJ 3071 Football
- POJ-3071-Football
- poj-3071-Football
- poj 3071 football
- POJ 1258 Agri-Net
- Caused by: tag 'select', field 'list', name 'Model.属性': The requested list key '@*.*.*.*.*.EmpModel@
- 度度熊与邪恶大魔王 DP | 完全背包
- OpenCV学习笔记(29)人脸识别 练习
- HDU 1520 Anniversary party
- POJ 3071 Football
- codeforces 450-B Jzzhu and Sequences 矩阵快速幂
- Java菜鸟面试突破系列之MySQL优化
- [spfa]单源最短路径
- 欧拉图的基本概念以及判定方法
- 饭卡
- 根据输入的银行卡号显示开户银行类型,并将银行卡号四位分隔
- 正整数分组
- shell demo analyse------ 2