'commit'
This commit is contained in:
44
TangDou/LuoGuBook/L16_1_2.cpp
Normal file
44
TangDou/LuoGuBook/L16_1_2.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user