[题解]AT_abc263_f [ABC263F] Tournament

先为大家毙掉一个错解

思路

首先不难发现,如果将整棵比赛的对战图画出来,一定是一个满二叉树。

不妨将令一个节点 \(u\) 的左右儿子编号分别为 \(2u\)\(2u + 1\)

然后定义 \(dp_{u,d}\) 表示将 \(u\) 为根的子树内的选手全部比赛完,并且 \(u\) 已经赢了 \(d\) 场的最大结果。发现对于 \(u\) 要么是从左子树中赢上来的,要么是从右子树中赢上来的,只需从中去一个最大值即可。

发现这个过程可以用 DFS 计算,并有较多的重复计算,加一个记忆化即可。

Code

#include <bits/stdc++.h>  
#define re register  
#define int long long  
#define pot(x) (1ll << x)  
  
using namespace std;  
  
const int N = 5e5 + 10,M = 24;  
int n,m;  
int arr[N][M],dp[N][M];  
  
inline int read(){  
    int r = 0,w = 1;  
    char c = getchar();  
    while (c < '0' || c > '9'){  
        if (c == '-') w = -1;  
        c = getchar();  
    }  
    while (c >= '0' && c <= '9'){  
        r = (r << 1) + (r << 3) + (c ^ 48);  
        c = getchar();  
    }  
    return r * w;  
}  
  
inline int dfs(int u,int d){  
    if (~dp[u][d]) return dp[u][d];  
    if (u >= pot(n)) return dp[u][d] = arr[u - pot(n) + 1][d];  
    return dp[u][d] = max(dfs(u << 1,d + 1) + dfs(u << 1 | 1,0),dfs(u << 1,0) + dfs(u << 1 | 1,d + 1));  
}  
  
signed main(){  
    memset(dp,-1,sizeof(dp));  
    n = read();  
    for (re int i = 1;i <= pot(n);i++){  
        for (re int j = 1;j <= n;j++) arr[i][j] = read();  
    }  
    printf("%lld",dfs(1,0));  
    return 0;  
}  
posted @ 2024-06-22 11:02  WBIKPS  阅读(31)  评论(0)    收藏  举报