POJ3071(Football)

Football

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3469   Accepted: 1782

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 offloat.

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

Stanford Local 2006

概率dp(简单的求概率)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define N 130
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
using namespace std;
#define MAXN 2
#define ll double
double p[N][N], win[10][N];

int main()
{
    int n;
    while(~scanf("%d", &n) && n != -1)
    {
        for(int i = 1; i <= (1 << n); i++)
            for(int j = 1; j <= (1 << n); j++)
            scanf("%lf", &p[i][j]);
        for(int i = 1; i <= (1 << n); i++) win[0][i] = 1.0;

        double t;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= (1 << n); j++) {
               int k = (j - 1) / (1 << (i - 1));
               t = 0.0;
               if(k % 2)
                  for(int q = (k - 1) * (1 << (i - 1)) + 1; q <= k * (1 << (i - 1)); q++)
                    t += win[i - 1][j] * win[i - 1][q] * p[j][q];
               else
                  for(int q = (k + 1) * (1 << (i - 1)) + 1; q <= (k + 2) * (1 << (i - 1)); q++)
                    t += win[i - 1][j] * win[i - 1][q] * p[j][q];
               win[i][j] = t;
            }

        double maxn = -1.0;
        int winner = 0;
        for(int i = 1; i <= (1 << n); i++)
            if(win[n][i] > maxn) maxn = win[n][i], winner = i;

        printf("%d\n", winner);

    }
    return 0;
}

时间: 2024-10-18 14:01:01

POJ3071(Football)的相关文章

【POJ 3071】 Football(DP)

[POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea

HTML5移动开发之路(27)—— JavaScript回顾2

本文为 兄弟连IT教育 机构官方 HTML5培训 教程,主要介绍:HTML5移动开发之路(27)-- JavaScript回顾2 JavaScript面向对象基础知识 1.如何定义一个类,使用如下语法来创建一个类 [javascript] view plain copy print? function Person(name, age){ //习惯上第一个字母大写 //this修饰的变量称为属性 this.name = name; this.age = age; //如果属性值是一个函数,则这个

SQL从零到迅速精通【实用函数(2)】

1.对查询结果进行排序 查询stu_info表中所有学生信息,并按照成绩由高到底进行排序,输入语句如下. SELECT * FROM stu_info ORDER BY s_score DESC;  --asc是指定列按升序排列,desc则是指定列按降序排列. 2.数据控制语句[授权.禁止和收回] (1)授权权限操作 对名称为guest的用户进行授权,允许其对stu_info数据表执行更新和删除的操作权限,输入语句如下. GRANT UPDATE,DELETE ON stu_info   --U

计算机科学及编程导论(7)数组及可变性、字典、伪代码,代码运行效率简介

1. 数组及可变性 当创建一个数组的时候,它将与一个对象进行绑定 L1 = [1, 2, 3] L2 = L1 L1[0] = 4 print(L2)#=>[4, 2, 3] L2 = L1 意味着L2与L1指向同一个对象,而L1[0]=4则改变了对象的值,所以最终L2的值也会改变,可以与下面这个例子进行比较 a = 1 #a指向对象1 b = a #b指向对象a a = 4 #此时a指向了对象4 print(b) #=>1,由于b依旧指向对象1,所以没有发生变化 2.字典 字典包括了以下几个

我的MYSQL学习心得(六)

我的MYSQL学习心得(六) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYSQL学习心得(五) 这一节主要介绍MYSQL里的函数,MYSQL里的函数很多,我这里主要介绍MYSQL里有而SQLSERVER没有的函数 数学函数 1.求余函数MOD(X,Y) MOD(X,Y)返回x被y除后的余数,MOD()对于带有小数部分的数值也起作用,他返回除法运算后的精确余数 SELECT MOD(31,8) 2.四舍五入函数TR

jQuery应用之(一)使用jQuery选择器(荐)

如上文(地址)jQuery预先的javascript的编程,提供了计划所有css3下的标准选择器,开发者可以利用这些选择器轻松选择各种元素,供javascript使用. 重要的是jQuery对这些选择器的兼容性特别好,主流的浏览器都测试通过,这使得理论上的css3选择器一下编程了事实.开发者可以按照以前的方法定义各种css类别,然后通过addClass()方法或者className属性将其添加到指定的元素集合中. 1.属性选择器 (属性可以参考jQuery文档或者http://www.w3sch

Java基础(十一):接口

一.接口: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念.类描述对象的属性和方法.接口则包含类要实现的方法.除非实现接口的类是抽象类,否则该类要定义接口中的所有方法. 接口无法被实例化,但是可以被实现.一个实现接口的类,必须实现接口内所描述的所有方法,否则就必须声明为抽象类.另外,在 Java 中,接口

我的IT梦——web前端开发之HTML,CSS(一)

HTML HTML全称HyperText Markup Language(超文本标记语言) 标签成对出现 <!DOCTYPE html>    文档类型定义 < >    标记||标签 charset=UTF-8    字符编码集,万国码 <head></head>    头部 <body></body>    内容 <!-- -->    注释 标题<h1></h1><h2></

视觉SLAM之词袋(bag of words) 模型与K-means聚类算法浅析(1)

在目前实际的视觉SLAM中,闭环检测多采用DBOW2模型https://github.com/dorian3d/DBoW2,而bag of words 又运用了数据挖掘的K-means聚类算法,笔者只通过bag of words 模型用在图像处理中进行形象讲解,并没有涉及太多对SLAM的闭环检测的应用. 1.Bag-of-words模型简介 Bag-of-words模型是信息检索领域常用的文档表示方法.在信息检索中,BOW模型假定对于一个文档,忽略它的单词顺序和语法.句法等要素,将其仅仅看作是若