1 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include<map> #include<set> #include<list> #include<queue> #include<stack> #include<ctime> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm>
#define MAXN 100005 #define MAX_INT 2147483647 #define FA(x) (x>>1) #define LC(x) ((x<<1)) #define RC(x) ((x<<1)|1) #define MID(x,y) ( (x + y) >> 1 ) #define TIME_BEGIN double t1 = clock() #define TIME_END double t2 = clock();printf("\n%.2lfms\n",t2-t1)
using namespace std;
int size,top = 1; int heap[MAXN];
bool cmp(int a,int b) { return a < b; }
void up(int id) { if(id == 1) return ; if(!cmp(heap[FA(id)] , heap[id])) { swap(heap[FA(id)] , heap[id]); up(FA(id)); } }
void down(int id) { if(RC(id) <= size) { if(cmp(heap[LC(id)] , heap[RC(id)]) && cmp(heap[LC(id)] , heap[id])) { swap(heap[LC(id)] , heap[id]); down(LC(id)); } else if(!cmp(heap[LC(id)] , heap[RC(id)]) && cmp(heap[RC(id)] , heap[id])) { swap(heap[RC(id)] , heap[id]); down(RC(id)); } } else if(LC(id) <= size) { if(cmp(heap[LC(id)] , heap[id])) { swap(heap[LC(id)] , heap[id]); down(LC(id)); } } }
void insert(int t) { heap[++size] = t; up(size); }
int dele() { int tmp = heap[top]; heap[top] = heap[size--]; down(top); return tmp; }
void print(int num) { int k = 1,cnt = 0,kk = 0; while(num) { num -= k; while(kk < k) { cnt ++; kk ++; cout<<heap[cnt]<<" "; } kk = 0; cout<<endl; k = min(k<<1, num); } }
int main() { TIME_BEGIN; freopen("./heap.in" , "r" , stdin); int t; while(cin>>t) { insert(t); } print(size); cout<<"==============="<<endl; while(size) { cout<<dele()<<" "; } cout<<endl<<"==============="<<endl; TIME_END; return 0; }
|