C++-LUOGU1059-明明的随机数[堆排序]

就是秀一波操作辣

 1 #include <iostream>
 2 using namespace std;
 3 const int N=1e3+20;
 4 int ans[N],cnt=1;
 5 struct heap{
 6     int a[N],n;    
 7     int top(){return a[1];}
 8     bool empty(){return n==0;}
 9     void push(int x){a[++n]=x,up(n);}
10     void pop(){swap(a[1],a[n--]),down(1);}
11     void up(int x){
12         for(int f;x>1,f=x>>1;x=f){
13             if(a[f]>a[x])swap(a[f],a[x]);
14             else return;
15         }
16     }
17     void down(int x){
18         for(int ls,rs,p;(x<<1)<=n;x=p){
19             ls=x<<1,rs=x<<1|1;
20             p=rs<=n&&a[rs]<a[ls]?rs:ls;
21             if(a[p]<a[x])swap(a[p],a[x]);
22             else return;
23         }
24     }
25 };
26 heap q;
27 int main(){
28     int n;cin>>n; 
29     for(int a;n--;)cin>>a,q.push(a);
30     for(int a;!q.empty();){
31         a=q.top(),q.pop();
32         if(ans[cnt-1]!=a)ans[cnt++]=a;
33     }
34     cout<<cnt-1<<endl;
35     for(int i=1;i<cnt;i++)cout<<ans[i]<<" ";
36     return 0;
37 }
原文地址:https://www.cnblogs.com/JasonCow/p/12454494.html