You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}