POJ3071 Football
嘟嘟嘟
这题还给样例解释了,那自然能想到dp。
因为比赛形式构成了一个树形结构,而且还和线段树的结构一模一样,那就索性这么dp:令\(dp[u][i]\)表示在节点\(u\),\(i\)获胜的概率。
然后我们枚举左右儿子谁赢,就很容易搞出转移方程:$$dp[u][i] = \sum dp[u << 1][i] * dp[u << 1 | 1][j] * p[i][j] \dp[u][i] = \sum dp[u << 1 | 1][i] * dp[u << 1][j] * p[i][j]$$。
当递归到叶子节点的时候,初始化\(dp[u][L] = 1\)。至于怎么判断叶子节点,就跟线段树一样。
看了很多题解都找到了一个什么规律,反正找规律方面我的水平是0……
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = (1 << 7) + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
}
int K, n;
db p[maxn][maxn], dp[maxn << 2][maxn];
In void dfs(int now, int L, int R)
{
for(int i = L; i <= R; ++i) dp[now][i] = 0;
if(L == R) {dp[now][L] = 1; return;}
int mid = (L + R) >> 1;
dfs(now << 1, L, mid), dfs(now << 1 | 1, mid + 1, R);
for(int i = L; i <= mid; ++i)
for(int j = mid + 1; j <= R; ++j)
dp[now][i] += dp[now << 1][i] * dp[now << 1 | 1][j] * p[i][j];
for(int i = mid + 1; i <= R; ++i)
for(int j = L; j <= mid; ++j)
dp[now][i] += dp[now << 1 | 1][i] * dp[now << 1][j] * p[i][j];
}
int main()
{
//MYFILE();
while(scanf("%d", &K) && ~K)
{
n = 1 << K;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j) scanf("%lf", &p[i][j]);
dfs(1, 1, n);
int pos = 1;
for(int i = 2; i <= n; ++i) if(dp[1][i] > dp[1][pos]) pos = i;
write(pos), enter;
}
return 0;
}