Maximum (small) heap initialization, insertion, deletion, and use of its sort implementation

Source: Internet
Author: User

#include <bits/stdc++.h>#definePB Push_back#defineMP Make_pair#defineESP 1e-8//#define Lson L, M, rt<<1//#define Rson m+1, R, Rt<<1|1#defineSZ (x) ((int) ((x). Size ()))#definePF (x) ((x) * (x))#definePB Push_back#definePi ACOs (-1.0)#defineIn Freopen ("Solve_in.txt", "R", stdin);#defineOut Freopen ("Solve_out.txt", "w", stdout);#defineBug (x) cerr << "line:" << (x) << ">>>>>>\n";#defineTL cerr << "time Elapsed:" << (double) clock ()/clocks_per_sec * + << "MS" << Endl;#defineINF 0x0f0f0f0fusing namespaceStd;typedefLong Longll;typedef unsignedLong Longull;typedef Map<ll,int>Mps;typedef pair<int,int>pii;typedef mps::iterator IT;classMaxheap { Public: Vector<int>Heap; intN; voidInit (vector<int>arr) {         This->n =sz (arr); Heap.resize (n+1);//HEAP[1...N] storing elements;         for(intindex =1; Index <= N; index++) {Heap[index]= arr[index-1]; }        intID =N;  while(Id >1) {Heapdown (id/2); ID-=2; }    }    voidHeapdown (intRT) {//cout << rt << Endl;        if(Rt > N)return; intLson = rt<<1; intRson = rt<<1|1; if(Lson > N)return; if(Rson > N | | heap[rson] <Heap[lson]) {            if(Heap[rt] <Heap[lson])                {Swap (Heap[rt], Heap[lson]);            Heapdown (Lson); }        } Else  {           if(Heap[rt] <Heap[rson])            {Swap (Heap[rt], Heap[rson]);           Heapdown (Rson); }        }    }    voidHeapup (intRT) {         while(Rt >1&& Heap[rt] > heap[rt/2]) {swap (Heap[rt], Heap[rt/2]); RT/=2; }    }    voidInsertheap (intvalue)        {HEAP.PB (value); N++;    Heapup (n); }    voidDeleteheap (intPOS) {        if(pos = =N) {heap.pop_back (); N--; } Else{swap (Heap[pos], heap[n]);            Heap.pop_back (); N--; if(pos = =1|| Heap[pos] < heap[pos/2]) {Heapdown (POS); } Else{heapup (POS); }        }    }    intGetmaxvalue () {if(N >=1)returnheap[1]; return-inf; } Vector<int>getsortedlist () {maxheap tmpheap= * This; Vector<int>ans;  while(TMPHEAP.N) {ANS.PB (Tmpheap.getmaxvalue ()); Tmpheap.deleteheap (1); }        returnans; }    voidprint () {vector<int> VEC =getsortedlist ();  for(inti =0; I < sz (VEC); i++) printf ("%d%c", Vec[i], I! = SZ (VEC)-1?' ':'\ n'); }    voidprintheap () { for(inti =1; I <= N; i++) printf ("%d%c", Heap[i], I! = n?' ':'\ n'); }} Solver;intMain () {inch    intN; Vector<int>VEC; CIN>>N;  for(inti =0; I < n; i++) {        intu; scanf ("%d", &u);//cout << u << endl;VEC.PB (U);    } solver.init (VEC);    Solver.printheap (); Solver.insertheap ( the);    Solver.printheap (); Solver.deleteheap (2);    Solver.printheap (); Solver.deleteheap (2);    Solver.printheap ();    Solver.print (); return 0;}
View Code

Maximum (small) heap initialization, insertion, deletion, and use of its sort implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.