1 #include<bits/stdc++.h>
2 using namespace std;
3 int len;
4 int myheap[12];
5 //堆排序每个子节点最多交换log次,所以堆排是nlogn;
6 void put(int x)
7 {
8 myheap[++len]=x;
9 int son=len;
10 while(son>1)
11 {
12 if(myheap[son]<myheap[son/2])//父节点》=子节点
13 {
14 swap(myheap[son],myheap[son/2]);
15 son/=2;
16 }
17 else break;
18 }
19 }
20
21 int getx()
22 {
23 int ans=0;
24 if(len>=1)ans=myheap[1];
25 else return -1;
26 myheap[1]=myheap[len--];
27 int fa=1;
28 while(2*fa<=len)//没有son节点跳出
29 {
30 int son=2*fa;
31 if(son<len&&myheap[son+1]<myheap[son])son++;
32 if(myheap[son]<myheap[fa])swap(myheap[son],myheap[fa]);
33 else break;
34 fa=son;
35 }
36 return ans;
37 }
38 int main()
39 {
40 put(3);
41 put(5);
42 put(1);
43 put(7);
44 put(6);
45 put(4);
46 put(2);
47 put(5);
48 put(4);
49 put(1);
50 cout<<len<<endl;
51 for(int i=1;i<=len;i++)cout<<myheap[i]<<" ";
52 cout<<endl;
53 for(int i=1;i<=12;i++)cout<<getx()<<" ";
54 return 0;
55 }