Pagini recente » Cod sursa (job #1508212) | Cod sursa (job #2895506) | Cod sursa (job #774951) | Clasament avram_iancu_9 | Cod sursa (job #1866373)
#include <fstream>
#include <vector>
using namespace std ;
ifstream fin ("algsort.in") ;
ofstream fout ("algsort.out") ;
class Heap {
public :
vector < int > Hheap ;
int sz ;
Heap ( int N )
{
Hheap.resize ( N ) ;
sz = 0 ;
}
void Push ( int NewNode ) {
++ sz ;
Hheap [sz] = NewNode ;
Up (sz) ;
}
int Top ()
{
return Hheap [1] ;
}
void Pop ()
{
swap (Hheap[1] , Hheap[sz--]) ;
Down(1) ;
}
bool Empty()
{
if ( sz != 0 )
return false ;
return true ;
}
private :
void Up ( int nod )
{
while ( ( nod >> 1 ) > 0 and Hheap [nod] < Hheap [nod>>1] ) {
swap (Hheap [nod] , Hheap [nod >> 1]) ;
nod >>= 1 ;
}
}
void Down ( int nod ) {
while ( (nod << 1) <= sz ) {
if ( Hheap [nod] > Hheap [nod<<1] and ( (nod << 1 |1) > sz or
Hheap [nod<<1|1] > Hheap [nod<<1] ) ) {
swap (Hheap [nod], Hheap [nod<<1]) ;
nod <<= 1 ;
}
else if ( ((nod <<1)|1) <= sz and Hheap [nod] > Hheap [nod << 1|1] ) {
swap (Hheap [nod], Hheap [nod <<1|1]) ;
nod <<= 1 ;
nod |= 1 ;
}
else {
break ;
}
}
}
};
Heap *H = new Heap (500014) ;
int main()
{
int n ;
fin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
int x ;
fin >> x ;
H -> Push (x) ;
}
while ( !H-> Empty() ) {
fout << H -> Top() << ' ' ;
H -> Pop() ;
}
return 0 ;
}