poj3071Football(概率期望dp)
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)的更多相关文章
- 【BZOJ-1419】Red is good 概率期望DP
1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 660 Solved: 257[Submit][Status][Di ...
- 【bzoj4832】[Lydsy2017年4月月赛]抵制克苏恩 概率期望dp
题目描述 你分别有a.b.c个血量为1.2.3的奴隶主,假设英雄血量无限,问:如果对面下出一个K点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输入 输入包含多局游戏. 第一行包含一个整数 T (T ...
- 【loj6191】「美团 CodeM 复赛」配对游戏 概率期望dp
题目描述 n次向一个栈中加入0或1中随机1个,如果一次加入0时栈顶元素为1,则将这两个元素弹栈.问最终栈中元素个数的期望是多少. 输入 一行一个正整数 n . 输出 一行一个实数,表示期望剩下的人数, ...
- Codeforces - 1264C - Beautiful Mirrors with queries - 概率期望dp
一道挺难的概率期望dp,花了很长时间才学会div2的E怎么做,但这道题是另一种设法. https://codeforces.com/contest/1264/problem/C 要设为 \(dp_i\ ...
- 概率期望dp
对于概率dp,我一直都弄得不是特别明白,虽然以前也有为了考试去突击过,但是终究还是掌握得不是很好,所以决定再去学习一遍,把重要的东西记录下来. 1.hdu4405 Description 在一个 \( ...
- Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)
题目链接:New Year and Arbitrary Arrangement 题意: 有一个ab字符串,初始为空. 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾 ...
- [BZOJ4832]抵制克苏恩(概率期望DP)
方法一:倒推,最常规的期望DP.f[i][a][b][c]表示还要再攻击k次,目前三种随从个数分别为a,b,c的期望攻击英雄次数,直接转移即可. #include<cstdio> #inc ...
- LightOJ 1030 Discovering Gold (概率/期望DP)
题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...
- 【POJ 2096】Collecting Bugs 概率期望dp
题意 有s个系统,n种bug,小明每天找出一个bug,可能是任意一个系统的,可能是任意一种bug,即是某一系统的bug概率是1/s,是某一种bug概率是1/n. 求他找到s个系统的bug,n种bug, ...
随机推荐
- ARM处理器的寄存器,ARM与Thumb状态,7中运行模式
** ARM处理器的寄存器,ARM与Thumb状态,7中运行模式 分类: 嵌入式 ARM处理器工作模式一共有 7 种 : USR 模式 正常用户模式,程序正常执行模式 FIQ模式(Fast ...
- 【sqli-labs】 less54 GET -Challenge -Union -10 queries allowed -Variation1 (GET型 挑战 联合查询 只允许10次查询 变化1)
尝试的次数只有10次 http://192.168.136.128/sqli-labs-master/Less-54/index.php?id=1' 单引号报错,错误信息没有显示 加注释符页面恢复正常 ...
- comdlg32.dll
dll的应用,目前还不知道要怎么查看dll里的功能,暂且试着用了一个, 下面的Declare 分32位office软件和64位,如果是64位,要在Declare 后面加上PtrSafe ,定义的Typ ...
- Mysql 在Linux下的安装
1.获取mysql源码 wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49.tar.gz 3.添加mysql用户和用户组,创建 ...
- word-spacing和letter-spacing区别
word-spacing:单词与单词间的间距 letter-spacing:字母与字母间的间距
- Vi/Vim基本用法
Vi/Vim是Linux中一款功能强大的编辑器,vi是Visual Interface的缩写,即可视化接口,vim是vi iMprove的缩写,即 vi的增强版(具有语法着色功能).它在Linux上的 ...
- Gym - 101611D Decoding of Varints(阅读理解题 )
Decoding of Varints 题意&思路: 首先根据红色边框部分的公式算出x,再有绿色部分得知,如果x是偶数则直接除以2,x是奇数则(x+1)/-2. PS:这题有数据会爆掉un ...
- BZOJ 2501 [usaco2010 Oct]Soda Machine
[题意概述] 给出一个[0,1,000,000,000]的整数数轴,刚开始每个位置都为0,有n个区间加操作,最后询问数轴上最大的数是多少. [题解] 我写的是离散化后线段树维护区间最值. 其实貌似不用 ...
- 【模板】非旋转Treap
Treap,也叫做树堆,是指有一个随机附加域满足堆的性质的二叉搜索树. 如果一棵二叉搜索树插入节点的顺序是随机的,那我们得到的二叉搜索树在大多数情况下是平衡的,期望高度是log(n). 但有些情况下我 ...
- mybatis源码阅读-执行器Executor(四)
说明 前面二看到了 sqlSession最终是找到MapperStatement然后委托给Executer执行的 Executer到底做了什么 接口定义 public interface Execut ...