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. 四次挥手与tcp标志位

    鉴于tcp的标志位可以同时置位,在相应端无数据传输时,四次握手可以用三次报文完成.

  2. day12-闭包函数、装饰器

    目录 闭包函数 装饰器 无参装饰器 有参装饰器 装饰器模板 闭包函数 之前我们都是通过参数将外部的值传给函数,而闭包打破了层级关系,把局部变量拿到全局使用,并把外部的变量封装到内部函数中,然后下次直接 ...

  3. 程序员不可不知的Linux性能工具

    前言 在实际开发中,有时候会收到一些服务的监控报警,比如CPU飙高,内存飙高等,这个时候,我们会登录到服务器上进行排查.本篇博客将涵盖这方面的知识:Linux性能工具. 一次线上问题排查模拟 背景:服 ...

  4. Django工程

    一.Django工程创建 1.Django安装: pip3 install django 安装成功后,会在python的安装目录下“Scripts"中生成”django-admin.exe& ...

  5. Enable ssh root login in Solaris

    1. Change the file /etc/ssh/sshd_config with PermitRootLogin yes to replace PermitRootLogin no 2. re ...

  6. (C/C++学习)7.数组及其访问方式

    说明:数组的数据类型是一种构造类型,而存储数组的内存是一段连续的存储区域.数组的数据类型决定了连续内存的访问方式,它包括数组的三要素:起始地址.步长以及元素个数. 一.一维数组 1.形式:type 数 ...

  7. Dinic当前弧优化 模板及教程

    在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...

  8. UVA1395 Slim Span(kruskal)

    题目:Slim Span UVA 1395 题意:给出一副无向有权图,求生成树中最小的苗条度(最大权值减最小权值),如果不能生成树,就输出-1: 思路:将所有的边按权值有小到大排序,然后枚举每一条边, ...

  9. svn 使用TortoiseSVN server搭建本地SVN服务器

    使用TortoiseSVN server搭建本地SVN服务器

  10. JSTL 实现 为Select赋多个值

    需要注意需要在.jsp文件中引入相应的类库 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core ...