P8681 题解
solution
题意简述
-
给定一个完全二叉树。
-
求同一深度的节点权值之和最大的深度(深度为第二关键字)。
-
完全二叉树的性质
设深度为
题目分析
有了以上的性质,我们不难想到解题思路:可以对序列
代码
#include <bits/stdc++.h>
using namespace std;
int n, a, sum, ans, dep = 1, Max = -1e9;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a;
sum += a;
if (i == (1 << dep)-1) {//若是末尾节点,切换到下一层
if (sum > Max) {//找到可行解
Max = sum;
ans = dep;
}
++dep;
sum = 0;
}
}
if (sum > Max) {//特判叶子节点
Max = sum;
ans = dep;
}
cout << ans;
return 0;
}