Files
python/TangDou/LuoGuBook/L16_1_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

44 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 260;
struct node {
int value;
int winner;
} nodes[N];
//本题n的概念很奇葩,不是n个国家,是2^n个国家~,比如n=3,就是2^3个国家,即8个国家。
int n;
//函数定义:计算x结点的获胜者
void dfs(int x) {
//叶子结点
if (x >= (1 << n)) return;
//左子树,右子树
dfs(2 * x), dfs(2 * x + 1);
//左结点值,右结点值
int lvalue = nodes[2 * x].value, rvalue = nodes[2 * x + 1].value;
//pk
if (lvalue > rvalue) nodes[x].value = lvalue, nodes[x].winner = nodes[2 * x].winner;
else nodes[x].value = rvalue, nodes[x].winner = nodes[2 * x + 1].winner;
}
/**
测试用例:
3
4 2 3 1 10 5 9 7
*/
int main() {
cin >> n;
for (int i = 0; i < (1 << n); i++) {//完美二叉树结点个数 2^n-1
int k = i + (1 << n);
cin >> nodes[k].value; //读入各个结点的能力值
nodes[k].winner = i + 1; //叶子结点的获胜方就是自己国家的编号
}
dfs(1);//从根结点开始遍历
//找亚军
cout << ((nodes[2].value > nodes[3].value) ? nodes[3].winner : nodes[2].winner) << endl;
return 0;
}