Football
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:2875 |
|
Accepted:1462 |
Description
Consider a single-elimination football tournament involving 2NTeams, 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. afterNRounds, only one team remains undefeated; this team is declared the winner.
Given a MatrixP= [Pij] Such thatPijIs the probability that teamIWill beat teamJIn 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 containingN(1 ≤N≤ 7). The next 2NLines each contain 2NValues; here,JTh value onITh line representsPij. The MatrixPWill satisfy the constraints thatPij= 1.0?PjiFor allI=J, AndPII= 0.0 for allI. 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 eitherdouble
Data Type insteadfloat
.
Output
The output file shoshould 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
20.0 0.1 0.2 0.30.9 0.0 0.4 0.50.8 0.6 0.0 0.60.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
Solution:
There are 2 ^ n to the power of a team. We know the probability that each team wins any two teams. The rules of the match the two adjacent teams, and we will continue to win the next round, direct elimination (two adjacent teams refer to, 1, 2, 4, 5, 6 ....... that is to say, 2 and 3 do not play. If 1 wins in 2, 2, and 3, and then wins in 3, 4, the final team won a total of N matches.
We use DP [I] [J] to indicate the probability that J will win in the I-th game, so there is a recursive equation DP [I] [J] = DP [I-1] [J] * DP [I-1] [k] * P [J] [K], J to participate in the I game to win, then J to participate in the I-1 game certainly won, so there are DP [I-] [J], in the I-th game, J's opponent is K and J wins, so P [J] [k] can play the I-th game, it means that K's I-1 won, so DP [I-1] [K].
The multiplication of the three elements is DP [I] [J].
So now there is a question: who is the opponent K in game I j?
We set the Team Number of 1-2 ^ n to 0-2 ^ n-1, assuming n = 3, then each team and the competition situation can be expressed
First game: 00 vs 01
First game: 10 Vs 11
First game: 100 vs 101
First game: 110 vs 111
Note that the number of matches mentioned here is not the total number of matches for all teams, but the number of matches for a certain team.
Observe that the team in the first 1st games starts from 0th places on the right.(The number of digits starts from 0, and the following is also true)The opposite is true.
The second competition below
Assume that the first game won by 00, 10, 100, and 111.
Second Game: 00 vs 10
Second Game: 100 vs 111
Observe that the number of teams in the 2nd match ranges from 1st.
Assume that 10 wins in the second game and 111 wins.
Third game: 10 vs 111
Observe that the number of teams in the 3rd match ranges from 2nd. 10 indicates 010
So I came to the conclusion that the second I-1 bit in the binary number of the team participating in the I match was the opposite.
For example, when I had J, K, then J> (I-1) ^ 1 = k> (I-1), j> (I-1) it means to remove the right number of J after the I-1 bit, the last bit became the original I-1 bit. ^ 1 is the inverse of it, 0 ^ 1 = 1 1 ^ 1 = 0
Code:
# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; const int maxn = (1 <7) + 1; double DP [8] [maxn]; // game I j wins Double P [maxn] [maxn]; int N; int main () {While (scanf ("% d", & N )! = EOF & n! =-1) {for (INT I = 0; I <(1 <n); I ++) for (Int J = 0; j <(1 <n); j ++) scanf ("% lf", & P [I] [J]); memset (DP, 0, sizeof (DP); // initialize for (Int J = 0; j <(1 <n); j ++) DP [0] [J] = 1; for (INT I = 1; I <= N; I ++) for (Int J = 0; j <(1 <n); j ++) for (int K = 0; k <(1 <n); k ++) {If (j> (I-1) ^ 1) = (k> (I-1) // binary second I-1 bit (starting from 0), different can be called opponent, ^ 1 is the inverse DP for the second I-1 bit [I] [J] + = DP [I-1] [J] * DP [I-1] [k] * P [J] [K ];} int ans; double max =-1; for (INT I = 0; I <(1 <n); I ++) if (DP [N] [I]> MAX) {ans = I + 1; max = DP [N] [I] ;}cout <ans <Endl ;} return 0 ;}