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. mysql跟java时间类型转换

    参照这个就行了,这个对应注入类型.===========java注入数据库==========java类型 mysql类型 成功与否date date yesdate time nodate time ...

  2. 数据结构与算法(6) -- heap

    binary heap就是一种complete binary tree(完全二叉树).也就是说,整棵binary tree除了最底层的叶节点之外,都是满的.而最底层的叶节点由左至右又不得有空隙. 以上 ...

  3. Sending Secret Messages LightOJ - 1404

    Sending Secret Messages LightOJ - 1404 Alice wants to send Bob some confidential messages. But their ...

  4. win10下VMware Workstation 14运行虚拟机黑屏

    最新 升级电脑Win10自动升级后,在打开虚拟机后,很卡,且出现黑屏,但是挂起后仍能显示锁屏界面,网上就一顿搜.下面放解决方法. 管理员运行 netsh winsock reset 帮你解决一切烦恼. ...

  5. .Net防sql注入的方法总结

    #防sql注入的常用方法: 1.服务端对前端传过来的参数值进行类型验证: 2.服务端执行sql,使用参数化传值,而不要使用sql字符串拼接: 3.服务端对前端传过来的数据进行sql关键词过来与检测: ...

  6. 3D全景之ThreeJs

    3D全景之ThreeJs 一.前言 随着H5越来越多的被应用到各个领域,3D也越来越频繁的出现在各个H5案例中,今天我们就来讨论一下3D全景的实现. 据百度百科上介绍:720全景是视角超过人的正常视角 ...

  7. mysql中的sql查询优化

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在where 及order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行null 值判断,否则将导致引擎放弃使用索引而进 ...

  8. Jzzhu and Numbers

    Jzzhu and Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. Drop all tables in MySQL database

    Drop all tables in MySQL database Answer: MySQL does not have a command for removing all database ta ...

  10. PatentTips – RDMA data transfer in a virtual environment

    BACKGROUND Embodiments of this invention relate to RDMA (remote direct memory access) data transfer ...