AcWing 1240. 完全二叉树的权值
原题链接
简单
作者:
Tanx_Y
,
2024-02-28 21:39:17
,
所有人可见
,
阅读 14
完全不一定满
C++ 代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;
int a[N];
struct tree{
int u;
long long sum;
}tr[20];
bool cmp(tree t1, tree t2)
{
if(t1.sum != t2.sum)
return t1.sum > t2.sum;
else
return t1.u < t2.u;
}
int pow(int u)
{
int ans = 1;
for(int i = 1; i <= u; i++)
ans *= 2;
return ans;
}
int n;
int main()
{
int i, j;
scanf("%d", &n);
for(i = 1; i <= n; i++)
scanf("%d", &a[i]);
for(i = 1; j <= n ; i++)
{
tr[i] = {i, 0};
j = pow(i-1);
int lim = min(n, pow(i) - 1);
while(j <= lim)
{
tr[i].sum += a[j++];
}
}
sort(tr+1, tr+i, cmp);
cout<<tr[1].u;
return 0;
}