试题 历届真题 完全二叉树的权值【第十届】【省赛】【B组】
资源限制
时间限制:1.0s 内存限制:256.0MB
完全二叉树每层的开头为2^(n-1) 结尾 2^n - 1
//完全二叉树每层的开头为2^(n-1) 结尾 2^n - 1
#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
long long maxx=-1e18;
int depth=1;
int ans=1;
for(int i=1;i<=n;i*=2)//头指针
{
long long s=0;
for(int j=i;j<=i*2-1&&j<=n;j++)
{
s+=a[j];
}
if(s>maxx)
{
ans=depth;
maxx=s;
}
depth++;
}
cout<<ans<<endl;
return 0;
}