Problem Statement

$2^N$ people, numbered $1$ to $2^N$, will participate in a rock-paper-scissors tournament.

The tournament proceeds as follows:

  • The participants are arranged in a row in the order Person $1$, Person $2$, $\ldots$, Person $2^N$ from left to right.
  • Let $2M$ be the current length of the row. For each $i\ (1\leq i \leq M)$, the $(2i-1)$-th and $(2i)$-th persons from the left play a game against each other. Then, the $M$ losers are removed from the row. This process is repeated $N$ times.

Here, if Person $i$ wins exactly $j$ games, they receive $C_{i,j}$ yen (Japanese currency). A person winning zero games receives nothing. Find the maximum possible total amount of money received by the $2^N$ people if the results of all games can be manipulated freely.

Constraints

  • $1 \leq N \leq 16$
  • $1 \leq C_{i,j} \leq 10^9$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$C_{1,1}$ $C_{1,2}$ $\ldots$ $C_{1,N}$
$C_{2,1}$ $C_{2,2}$ $\ldots$ $C_{2,N}$
$\vdots$
$C_{2^N,1}$ $C_{2^N,2}$ $\ldots$ $C_{2^N,N}$

Output

Print the answer.


Sample Input 1

2
2 5
6 5
2 1
7 9

Sample Output 1

15

The initial row of the people is $(1,2,3,4)$.

If Person $2$ wins the game against Person $1$, and Person $4$ wins the game against Person $3$, the row becomes $(2,4)$.

Then, if Person $4$ wins the game against Person $2$, the row becomes $(4)$, and the tournament ends.

Here, Person $2$ wins exactly $1$ game, and Person $4$ wins exactly $2$ games, so they receive $0+6+0+9=15$ yen in total, which is the maximum possible sum.


Sample Input 2

3
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

这种比赛,我们可以以满二叉树的方式表现比赛的过程。以叶子节点作为每个参赛者。然后对于一棵子树,他的根节点代表整颗子树赛后的获胜者。

我们不能知道这棵树每个节点是哪位参赛者,但我们可以通过这棵树的形式来做 dp。

满二叉树我们可以用线段树类似的方式给每个节点编号。易得,如果一个叶子节点编号为 \(x\),那么他代表第 \(x-2^n\) 号参赛者。一个参赛者赢得场数要从他输的那场的前面算。

定义 \(dp_{i,j}\) 为现在到了第 \(i\) 号节点,这名参赛者参加了 \(i\) 次比赛。转移时我们枚举是左子树的参赛者赢了还是右子树的参赛者赢了就好了。加个记忆化。

想到了代码非常好写。但之前完全没往这棵树上想过。

#include<bits/stdc++.h>
using namespace std;
const int N=17;
int n,c[1<<N][N];
long long dp[1<<N][N];
long long dfs(int x,int y)
{
if(x>=(1<<n))
return c[x^(1<<n)][y];
if(~dp[x][y])
return dp[x][y]; return dp[x][y]=max(dfs(x<<1,y+1)+dfs(x<<1|1,0),dfs(x<<1|1,y+1)+dfs(x<<1,0));
}
int main()
{
memset(dp,-1,sizeof(dp));
scanf("%d",&n);
for(int i=0;i<(1<<n);i++)
for(int j=1;j<=n;j++)
scanf("%d",c[i]+j);
printf("%lld",dfs(1,0));
return 0;
}

[ABC263F] Tournament的更多相关文章

  1. Codeforces CF#628 Education 8 A. Tennis Tournament

    A. Tennis Tournament time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Rock-Paper-Scissors Tournament[HDU1148]

    Rock-Paper-Scissors TournamentTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  3. CF 628A --- Tennis Tournament --- 水题

    CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m & ...

  4. ural 1218. Episode N-th: The Jedi Tournament

    1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Je ...

  5. URAL 1218 Episode N-th: The Jedi Tournament(强连通分量)(缩点)

    Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Kni ...

  6. Educational Codeforces Round 13 E. Another Sith Tournament 概率dp+状压

    题目链接: 题目 E. Another Sith Tournament time limit per test2.5 seconds memory limit per test256 megabyte ...

  7. CodeForce 356A Knight Tournament(set应用)

     Knight Tournament time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. 遗传算法selection总结-[Fitness, Tournament, Rank Selection]

    假设个体(individual)用\(h_i\)表示,该个体的适应度(fitness)为\(Fitness(h_i)\),被选择的概率为\(P(h_i)\). 另外假设种群(population)的个 ...

  9. 【CF913F】Strongly Connected Tournament 概率神题

    [CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...

  10. 【CF878C】Tournament set+并查集+链表

    [CF878C]Tournament 题意:有k个项目,n个运动员,第i个运动员的第j个项目的能力值为aij.一场比赛可以通过如下方式进行: 每次选出2个人和一个项目,该项目能力值高者获胜,败者被淘汰 ...

随机推荐

  1. 微服务集成redis并通过redis实现排行榜的功能

    默认你已经看过我之前的教程了,并且拥有上个教程完成的项目, 之前的教程 https://www.cnblogs.com/leafstar/p/17638933.html 由于redis的安装网上教程很 ...

  2. uni微信小程序隐私协议

    最近小程序又新增了个 隐私协议弹窗.需要用户去授权,官网的一些API才能使用.官网地址 功能展示 项目地址:https://ext.dcloud.net.cn/plugin?id=14358 1.ma ...

  3. SQL select关联表查询 统计另一个表合计

    db_order 是记录订单的,  一个订单一条记录.(oid, 运费, 实收金额, 产品KEY.......) db_soid  是记录出售商品的 (id, 商品名称, 售价, 数量, 成本, 标识 ...

  4. .Net8 AOT+VMP简单的逆向分析

    1.前言 测试下VMP加密.NET的强度,选了最新的.Net8+AOT编译,用VMP给它加壳.最后逆向下,简单的分析,本篇看下. 2.概述 一.前奏 首先一段简单的C#代码: namespace Te ...

  5. 如何使用Vite创建Vue3的uniapp项目

    项目结构 my-vue3-project ├─ .env //默认环境变量 ├─ .env.development //开发环境变量 ├─ .eslintrc-auto-import.json //( ...

  6. Solution -「洛谷 P5659」「CSP-S 2019」树上的数

    Description Link. 联赛原题应该都读过吧-- Solution Part 0 大致思路 主要的思路就是逐个打破,研究特殊的数据得到普通的结论. Part 1 暴力的部分分 暴力的部分分 ...

  7. Go指针探秘:深入理解内存与安全性

    Go指针为程序员提供了对内存的深入管理能力,同时确保了代码的安全性.本文深入探讨了Go指针的基础概念.操作.深层理解及其特性与限制.通过深入了解其设计哲学和应用,我们可以更好地利用Go的强大功能. 关 ...

  8. 8.2 BeingDebugged

    BeingDebugged 是Windows系统PEB结构体中的一个成员,它是一个标志位,用于标识当前进程是否正在被调试.BeingDebugged的值为0表示当前进程未被调试,值为1表示当前进程正在 ...

  9. 2023-10-07:用go语言,给定n个二维坐标,表示在二维平面的n个点, 坐标为double类型,精度最多小数点后两位, 希望在二维平面上画一个圆,圈住其中的k个点,其他的n-k个点都要在圆外。

    2023-10-07:用go语言,给定n个二维坐标,表示在二维平面的n个点, 坐标为double类型,精度最多小数点后两位, 希望在二维平面上画一个圆,圈住其中的k个点,其他的n-k个点都要在圆外. ...

  10. BizSpring在线商城常见问题

    一.什么是BizSpring在线商城? BizSpring在线商城是一个用java语言开发的完全开源的网络商城平台.该项目已经经历多次迭代升级是一个的成熟的在线商城解决方案,它具有轻量级,易于维护,操 ...