Pagini recente » Cod sursa (job #2043082) | Cod sursa (job #2546570) | Cod sursa (job #117195) | Cod sursa (job #1015137) | Cod sursa (job #2290663)
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("heapuri.in") ;
ofstream g("heapuri.out") ;
int a[30000] , b[30000] , n , t ;
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 ) ;
}
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 -- ;
}
}
}