Complete binary tree weights 2019 Tenth Blue Bridge cup G

Questions G: complete binary weights
time limit: 1.0s memory limit: 256.0MB out this question: 20 minutes
[Problems] Description
Given N nodes comprising a complete binary tree , each node of the tree has a weight in order from the
top to bottom, left to right, followed by a 1, a 2, ··· AN , as shown below:

Right now the nodes of the same depth Xiao Ming put values together, he wanted to know which node depth of
the weighted sum and the maximum? If you have more weight and depth with the maximum, minimum depth you output them.
Note: the depth of the root is 1.
[Input format
of the first row contains an integer N.
The second line contains N integers A 1, A 2, ··· AN .
[] Output format
output representative of an integer answer.
[] Sample input
. 7
. 1. 6. 5. 4. 3. 1 2

[Output] Sample
2
[Evaluation] and conventions used in Example scale
for evaluating all use cases, 1 ≤ N ≤ 100000, -100000 ≤ A i ≤ 100000.

analysis

① because the topic that is a complete binary tree, it can be stored with the order, pay attention subscript 1 from the start;

② two-cycle calculating weights of each node, to record the maximum;

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  const  int MAXN = 1000 ;
 . 4  int main () {
 . 5      int A [MAXN], C [ 21 is ] = { 0 }; // A for sequentially weight storage trees, c to the right depth value corresponding to the stored tree 
. 6      int n-; // K is the depth 
. 7      Scanf ( " % D " , & n-);
 . 8      for ( int I = . 1 ; I <= n-; I ++ ) {
 . 9          Scanf ( " % D ",&a[i]);
10     } 
11     c[1]=a[1];//第一层单独处理 
12     int k=2;
13     int ans=1;
14     for(int i=2;i<=n;i=i+pow(2,k-2)){
15         for(int j=i;(j<i+pow(2,k-1))&&(j<=n);j++){
16             c[k]+=a[j];
17         }
18         if(c[k]>c[k-1]) ans=k;
19         k++;
20     }
21     cout<<c[4]<<endl;
22     printf("%d",ans);
23 }

 

Guess you like

Origin www.cnblogs.com/taiga/p/12501160.html