堆中的路径

堆中的路径

 

 AC_Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1010;
 4 const int minn = -10001;
 5 
 6 int Heap[maxn];
 7 int _size;
 8 
 9 void init(){
10     _size = 0;
11     Heap[_size] = minn;
12 }
13 
14 void Insert(int x){
15     int i;
16     _size++;
17     for(i=_size; Heap[i/2]>x; i/=2){
18         Heap[i] = Heap[i/2];
19     }
20     Heap[i] = x;
21 }
22 
23 void Print(int i){
24     cout<<Heap[i]<<" ";
25 }
26 
27 
28 int main()
29 {
30     int n,m; scanf("%d%d",&n,&m);
31     init();
32     for(int i=0;i<n;i++){
33         int x; scanf("%d",&x);
34         Insert(x);
35     }
36     for(int i=0;i<m;i++){
37         int x; scanf("%d",&x);
38         for( ;x>1;x/=2){
39             Print(x);
40         }
41         cout<<Heap[1]<<endl;
42     }
43     return 0;
44 }

 

posted @ 2020-11-27 12:17  swsyya  阅读(92)  评论(0编辑  收藏  举报

回到顶部