Cod sursa(job #2198676)

Utilizator deliricnagisa deliric Data 24 aprilie 2018 23:53:41
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
using namespace std;
int n,v,i,heap[1002];
void push(int x)
{
    heap[++heap[0]]=x;
    int now=heap[0],next=heap[0]/2;
    while(next>=1)
        if(heap[now]<heap[next])
        {
            swap(heap[now],heap[next]);
            now/=2;
            next/=2;
        }
        else break;
}
void pop()
{
    heap[1]=heap[heap[0]--];
    int now=1,next=2;
    while(next<=heap[0])
    {
        if(next<heap[0]&&heap[next]>heap[next+1]) ++next;
        if(heap[now]>heap[next])
        {
            swap(heap[now],heap[next]);
            now=next;
            next*=2;
        }
        else break;
    }
}
int main()
{
    cin>>n;
    for(i=1;i<=n;++i)
    {
        cin>>v;
        push(v);
    }
    for(i=1;i<=n;++i)
    {
        cout<<heap[1]<<' ';
        pop();
    }
    return 0;
}