31,102 Fork Pile Exercise 3
time limit: 3 sspace limit: 128000 KBtitle level: Golden Gold SolvingTitle Description
Description
Given n (n≤500,000) and n integers (more ordered), the output is sorted.
Enter a description
Input Description
N and N integers
Output description
Output Description
n Integers (Ascending)
Sample input
Sample Input
5
12 11 10) 8 9
Sample output
Sample Output
8 9 10) 11 12
Data range and Tips
Data Size & Hint
For 33% of data n≤10000
For another 33% of the data n≤100,000 0≤ per number ≤1000
For 100% of data n≤500,000 0≤ per number ≤2*10^9
Ideas:
Handwritten heap;
Come on, on the code:
#include <cstdio>#include<iostream>#include<algorithm>using namespacestd;classT_heap {Private: intheap[500001],n; Public: voidUpintNow ) { if(now<=1)return ; intFront=now>>1; if(heap[front]>Heap[now]) {swap (Heap[front],heap[now]); Up (front); } } voidDownintNow ) { if(now>n)return; intvlc,vrc,next=Now ; BOOLBLC,BRC; if((now<<1) <=n) blc=true,vlc=heap[now<<1]; ElseBlc=false; if((now<<1|1) <=n) brc=true,vrc=heap[now<<1|1]; ElseBrc=false; if(BLC) {if(vlc<Heap[next]) {Next=now<<1; } } if(BRC) {if(vrc<Heap[next]) {Next=now<<1|1; } } if(next!=Now ) {swap (Heap[next],heap[now]); Down (next); } } voidPushintcur_) {N++; Heap[n]=Cur_; Up (n); } voidpop () {heap[1]=Heap[n]; N--; Down (1); } intTop () {returnheap[1]; }};classt_heap Heap;intN,ai;intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%d",&AI); Heap.push (AI); } for(intI=1; i<=n;i++) {printf ("%d", Heap.top ()); Heap.pop (); } return 0;}
AC Diary--Two fork pile exercises 3 Codevs 3110