题目
题解
数据结构。
一开始以为要bfs,后来发现是完全二叉树,直接循环即可。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100100;
int n, a[N], sum[N];
int main()
{
cin >> n;
for (int i = 1;i <= n;i ++) cin >> a[i];
int max_dp = (int) log2(n), max_w = 0;
for (int i = 0, k = 1;i <= max_dp;i ++) {
for (int j = 0;j < (1 << i) && k <= n;j ++, k ++)
sum[i+1] += a[k];
max_w = max (max_w, sum[i+1]);
}
for (int i = 1;i <= max_dp+1;i ++)
if (max_w == sum[i])
return cout << i << endl, 0;
return 0;
}