poj 3071 Football

Title:

Given an n, there are now 2 to the nth power of teams.

In the first round, 1-2, 3-4, 5-6. . . .

In the second round, the winners are sorted from small to large, and then the first and the second are compared, and the third and the fourth are compared. . . .

。。。。。

Ask which team has the most hope of winning the championship.

Ideas:

To find the probability, then recursively recursively dp.

Obviously, there are only n rounds in the competition, and each team has a range of team numbers in each round.

For example, in the second round, 2 can only be compared with 3, 4; 3 can only be compared with 1, 2. . . .

Assuming that dp[i][j] represents the probability of the i-th team winning in the jth round, then dp[i][j] = dp[i][j-1] * (Σ(dp[k] [j-1] * p[i][k])), k has a range and can be obtained by observation.

Code:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 double dp[1000][10];
 6 double p[1000][1000];
 7 int num[10];
 8 int main()
 9 {
10     int n;
11     num[0] = 1;
12     for (int i = 1;i < 10;i++) num[i] = 2 * num[i-1];
13     while (scanf("%d",&n) != EOF && ~n)
14     {
15         int m = num[n];
16         for (int i = 0;i < m;i++)
17         {
18             for (int j = 0;j < m;j++) scanf("%lf",&p[i][j]);
19         }
20         memset(dp,0,sizeof(dp));
21         for (int i = 0;i < m;i++)
22         {
23             if (i % 2)
24             {
25                 dp[i][1] = p[i][i-1];
26             }
27             else
28             {
29                 dp[i][1] = p[i][i+1];
30             }
31         }
32         for (int j = 2;j <= n;j++)
33         {
34             int cur = num[j-1];
35             for (int i = 0;i < m;i++)
36             {
37                 int id = i / cur;
38                 if (id % 2)
39                 {
40                     id--;
41                 }
42                 else
43                 {
44                     id++;
45                     
46                 }
47                 int l = id * cur;
48                 int r = l + cur;
49                 double tmp = 0;
50                 for (int k = l;k < r;k++)
51                 {
52                     tmp += dp[k][j-1] * p[i][k];
53                 }
54                 dp[i][j] = dp[i][j-1] * tmp;
55             }
56         }
57         int ans = 0;
58         for (int i = 1;i < m;i++)
59         {
60             if (dp[i][n] > dp[ans][n]) ans = i;
61         }
62         printf("%d\n",ans+1);
63     }
64     return 0;
65 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325711068&siteId=291194637