算法1
(规律)
完全二叉树的每一层第一个结点编号为2^(n-1),所以这一层结点从2^(n-1)----2^n,不包括2^n,属于下一层了,记得开ll就行了。
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+9;
typedef long long ll;
int w[N];
int n;
int main()
{
cin>>n;
int ans=0;
ll maxv=-1e9;
memset(w,0,sizeof w);
for(int i=1;i<=n;i++) cin>>w[i];
for(int dep=1;dep<=log2(n+1)+1;dep++)//细节 log2(n+1)+1;,自己可以测试一下,其实多一层也没事
{
ll temp=0;//记得 maxv 也开ll要不然爆int
for(int j=pow(2,dep-1);j<(pow(2,dep)>n ? n : pow(2,dep));j++)
{
temp+=w[j];
}
if(temp > maxv)
{
ans=dep;
maxv=temp;
}
}
cout<<ans;
}