AC Diary--Two fork pile exercises 3 Codevs 3110

Source: Internet
Author: User

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

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.