Files
exercises/dsa/ch5_binary_tree/luogu_P4715_matches.cpp
T
2026-10-16 10:21:53 +08:00

63 lines
2.0 KiB
C++

#include <cmath>
#include <functional>
#include <iostream>
class BinaryTree {
public:
struct Node {
int value = -1;
Node *father = nullptr, *left = nullptr, *right = nullptr;
int depth = -1;
};
Node* root = new Node({.depth = 0});
void init_full_tree(int height, const std::function<bool(int, int)>& comp) {
int id = 0;
return init_full_tree(height, comp, 0, nullptr, id);
}
protected:
void init_full_tree(int height, const std::function<bool(int, int)>& comp,
int current_depth, Node* current_node, int& id) {
if (current_depth == 0) { current_node = root; }
if (current_depth == height) {
current_node->value = id++;
return;
}
current_node->left = new Node();
current_node->right = new Node();
init_full_tree(height, comp, current_depth + 1, current_node->left, id);
init_full_tree(height, comp, current_depth + 1, current_node->right, id);
if (comp(current_node->left->value, current_node->right->value)) {
current_node->value = current_node->right->value;
} else {
current_node->value = current_node->left->value;
}
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
BinaryTree binary_tree;
std::vector<int> strengths;
std::cin >> n;
strengths.resize(static_cast<std::size_t>(std::pow(2, n)));
for (int& strength : strengths) { std::cin >> strength; }
binary_tree.init_full_tree(n,
[strengths](int i, int j) {
return strengths[i] < strengths[j];
});
// std::cout << binary_tree.root->value << std::endl;
int gold = binary_tree.root->value;
int silver = binary_tree.root->left->value == gold
? binary_tree.root->right->value
: binary_tree.root->left->value;
std::cout << silver + 1 << std::endl;
return 0;
}