Cod sursa(job #2290671)

Utilizator Dragomiralexandru621@yahoo.comDragomir ionut alexandru [email protected] Data 26 noiembrie 2018 20:16:26
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("heapuri.in") ;
ofstream g("heapuri.out") ;
int a[300000] , b[300000] , n , t , c[300000];
 int nr = 0 , nrp = 0 ;

void adauga( int x )
{
    while( x / 2 != 0 && a[x] < a[x / 2] )
    {
        int aux ;
        aux = a[x] ;
        a[x] = a[x/2] ;
        a[x/2] = aux ;

         aux = b[x/2] ;
         b[x/2] = b[x] ;
         b[x] = aux ;
        x /= 2 ;
    }
}

void sterge ( int x )

{
	int aux, y = 0;

	while ( x != y )
	{
		y = x;
		if ( y * 2 <= nr /* && a[x] > a[y * 2]*/ ) x = y * 2 ;
		if ( y * 2 + 1 <= nr && a[x] > a[y * 2 + 1] ) x = y * 2 + 1 ;

		aux = a[x] ;
		a[x] = a[y] ;
		a[y] = aux;

        aux = b[y] ;
        b[y] = b[x] ;
        b[x] = aux ;
	}
}


int main()
{ int nrelem ;
 f >> n ;
 for( int i = 1 ; i <= n ; i ++ )
 {  int caz  ;
     f >> caz ;
     if ( caz == 1 )
     {   nrp ++ ;
         f >> a[++nr] ;
         b[nr] = nrp ;
         adauga( nr ) ;
         c[b[nr]] = nr ;
     }
     if ( caz == 3 )
        g << a[1] << "\n" ;
     if ( caz == 2 )
     {
         f >> nrelem ; int j = 1 ;

         while( b[j] != nrelem )
            j ++ ;
         sterge( j ) ;// g << a[j] << " sters " ;
         nrp -- ;
         nr -- ;
     }

 }
}