Football

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5620   Accepted: 2868

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 i ≠ j, 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

Hint

In the test case above, teams 1 and 2 and teams 3 and 4 play against each other in 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)
p21p34p23 + p21p43p24
= 0.9 · 0.6 · 0.4 + 0.9 · 0.4 · 0.5 = 0.396.

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

Source

 
/*
因为2^n个球队 需要n大轮比赛才能决定冠军!
因此,可以用dp[i][j],表示第i大轮比赛,j球队赢得概率!
先遍历比赛轮数i,在遍历j,在遍历k,k表示j可以战胜的球队!
当判断j和k相邻时(可以打比赛),
dp[i][j] +=dp[i-1][j] * dp[i-1][k] * p[j][k];
表示在上一轮中,j和k都存活了下来,并且在这一轮中j战胜了k。
这样就解决了!
那么 如何判断两个球队是否相邻呢!
用到了^运算符,有一个性质 (2n) ^ (1) = 2n+1; (2n+1) ^ (1) = 2n
因此先给每一个数 >> (i-1),在进行^运算!就可以判断是否相邻了。
这个说不太好说明,写一下就很明了了!
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std; double dp[][];//dp[i][j]表示在第i场比赛中j胜出的概率
double p[][];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==-)break;
memset(dp,,sizeof(dp));
for(int i=;i<(<<n);i++)
for(int j=;j<(<<n);j++)
scanf("%lf",&p[i][j]);
//cin>>p[i][j];
for(int i=;i<(<<n);i++)dp[][i]=;
for(int i=;i<=n;i++)//2^n个人要进行n场比赛
{
for(int j=;j<(<<n);j++)
{
int t=j/(<<(i-));
t^=;
dp[i][j]=;
for(int k=t*(<<(i-));k<t*(<<(i-))+(<<(i-));k++)
dp[i][j]+=dp[i-][j]*dp[i-][k]*p[j][k];
}
}
int ans;
double temp=;
for(int i=;i<(<<n);i++)
{
if(dp[n][i]>temp)
{
ans=i;
temp=dp[n][i];
}
}
printf("%d\n",ans+);
}
return ;
}

poj3071Football(概率期望dp)的更多相关文章

  1. 【BZOJ-1419】Red is good 概率期望DP

    1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 660  Solved: 257[Submit][Status][Di ...

  2. 【bzoj4832】[Lydsy2017年4月月赛]抵制克苏恩 概率期望dp

    题目描述 你分别有a.b.c个血量为1.2.3的奴隶主,假设英雄血量无限,问:如果对面下出一个K点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输入 输入包含多局游戏. 第一行包含一个整数 T (T ...

  3. 【loj6191】「美团 CodeM 复赛」配对游戏 概率期望dp

    题目描述 n次向一个栈中加入0或1中随机1个,如果一次加入0时栈顶元素为1,则将这两个元素弹栈.问最终栈中元素个数的期望是多少. 输入 一行一个正整数 n . 输出 一行一个实数,表示期望剩下的人数, ...

  4. Codeforces - 1264C - Beautiful Mirrors with queries - 概率期望dp

    一道挺难的概率期望dp,花了很长时间才学会div2的E怎么做,但这道题是另一种设法. https://codeforces.com/contest/1264/problem/C 要设为 \(dp_i\ ...

  5. 概率期望dp

    对于概率dp,我一直都弄得不是特别明白,虽然以前也有为了考试去突击过,但是终究还是掌握得不是很好,所以决定再去学习一遍,把重要的东西记录下来. 1.hdu4405 Description 在一个 \( ...

  6. Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)

    题目链接:New Year and Arbitrary Arrangement 题意: 有一个ab字符串,初始为空. 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾 ...

  7. [BZOJ4832]抵制克苏恩(概率期望DP)

    方法一:倒推,最常规的期望DP.f[i][a][b][c]表示还要再攻击k次,目前三种随从个数分别为a,b,c的期望攻击英雄次数,直接转移即可. #include<cstdio> #inc ...

  8. LightOJ 1030 Discovering Gold (概率/期望DP)

    题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...

  9. 【POJ 2096】Collecting Bugs 概率期望dp

    题意 有s个系统,n种bug,小明每天找出一个bug,可能是任意一个系统的,可能是任意一种bug,即是某一系统的bug概率是1/s,是某一种bug概率是1/n. 求他找到s个系统的bug,n种bug, ...

随机推荐

  1. python发送文本邮件

    #!/usr/bin/env python #coding=utf-8 #Author: Ca0Gu0 import time import smtplib from email.mime.text ...

  2. Codeforces_718A

    A. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...

  3. HDU_2203_KMP入门

    亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. Redis 之order set有序集合结构及命令详解

    1.zadd key score1 value1 score2 value2  添加元素 2.zrem key value1 value2 ..  删除集合中的元素 3.zremrangebyscor ...

  5. viod 0是什么?

    之前在牛客网上看到别人用viod 0来代替undefined,所以我去网上搜了一下,MDN是这么说的: 这个运算符能向期望一个表达式的值是undefined的地方插入会产生副作用的表达式. void ...

  6. Vue中.sync修饰符

    Vue 中 sync的作用 <FatherComponent :a.sync = 'b'><FatherComponent /> 子组件中emit('update:a',... ...

  7. js  计算是今天多少周

    /** * 判断年份是否为润年 * * @param {Number} year */ function isLeapYear(year) { return (year % 400 == 0) || ...

  8. Java报表统计导出Word-xdocin方式

    官网:http://www.xdocin.com Controller层: //创建对象 XDocService xdocService = new XDocService(); //封装参数 Map ...

  9. 02. 爬取get请求的页面数据

    目录 02. 爬取get请求的页面数据 一.urllib库 二.由易到难的爬虫程序: 02. 爬取get请求的页面数据 一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用 ...

  10. ubuntu16.04 安装 eclipse

    从官网下载 eclipse 的 linux 版本 eclipse-cpp-neon-1a-linux-gtk-x86_64.tar.gz 直接解压就能得到一个可运行的IDE,但是直接点击 eclips ...