You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
p/lqb-19-b-cplus-binary-tree/
问题描述 给定一棵包含N 个节点的完全二叉树,树上每个节点都有一个权值,按从 上到下、从左到右的顺序依次是A1, A2,…… AN,如下图所示:\n现在小明要把相同深度的节点的权值加在一起,他想知道哪个深度的节点 权值之和最大?如果有多个深度的权值和同为最大,请你输出其中最小的深度。\n注:根的深度是1。\n输入格式 第一行包含一个整数N。 第二行包含N 个整数A1, A2, …… AN 。\n输出格式 输出一个整数代表答案。\n样例输入 1 2 7 1 6 5 4 3 2 1 样例输出 1 2 评测用例规模与约定 对于所有评测用例,1 <= N <= 100000,100000 <= Ai <=100000。\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define INF 0x3f3f3f3f #define N 100005 int num[N]={0}; int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&num[i]); int ans=1,k=0,max=-INF; for(int i=1;i<=ceil(log(n+1)/log(2));i++) { int sum=0; for(int j=0;j<pow(2,i-1);j++) sum+=num[k++]; if(sum>max) { max=sum; ans=i; } } printf("%d\n",ans); return 0; }
https://blog.debuginn.com/p/lqb-19-b-cplus-binary-tree/
Beta Was this translation helpful? Give feedback.
All reactions