Cod sursa(job #2464009)

Utilizator Radu_FilipescuFilipescu Radu Radu_Filipescu Data 28 septembrie 2019 13:34:58
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <fstream>

using namespace std;

ifstream fin( "heapuri.in" );
ofstream fout( "heapuri.out" );

const int NMAX = 200002;

int N;
int heap[NMAX], h_size;
int ord[NMAX], nr_ord;

void BubbleUp( int nod )
{
    if( nod == 1 ) return;

    if( heap[nod / 2] > heap[nod] )
    {
       swap( heap[nod / 2], heap[nod] );
       BubbleUp( nod / 2 );
    }
}

void BubbleDown( int nod )
{
    int v1, v2;

    if( nod * 2 > h_size ) return;
    else v1 = heap[nod * 2];

    if( nod * 2 + 1 > h_size ) v2 = 2000000000;
    else v2 = heap[nod * 2 + 1];

    if( min( v1, v2 ) > heap[nod] ) return;

    if( v1 < v2 )
    {
       swap( heap[nod], heap[nod * 2] );
       BubbleDown( nod * 2 );
    }
    else
    {
       swap( heap[nod], heap[nod * 2 + 1] );
       BubbleDown( nod * 2 + 1 );
    }
}

int HeapTop()
{
    if( h_size == 0 ) return -1;
    else return heap[1];
}

void HeapDel( int nod )
{
    heap[nod] = heap[h_size];
    --h_size;

    BubbleDown( nod );
}

void HeapAdd( int x )
{
    heap[ ++h_size ] = x;
    BubbleUp( h_size );
}

void Read()
{
    fin >> N;

    for( int i = 1; i <= N; ++i )
    {
       int t, x;

       fin >> t;
       if( t == 1 || t == 2 ) fin >> x;

       if( t == 1 ) { HeapAdd( x ); ord[++nr_ord] = x; }
       if( t == 2 )
       {
          for( int i = 1; i <= h_size; ++i )
            if( heap[i] == ord[x] ) HeapDel( i );
       }
       if( t == 3 ) fout << HeapTop() << '\n';
    }
}

int main()
{
    Read();

    return 0;
}