Pagini recente » Cod sursa (job #191043) | Cod sursa (job #194028) | Cod sursa (job #805518) | Monitorul de evaluare | Cod sursa (job #1514246)
#include <cstdio>
using namespace std;
FILE *f = fopen ( "heapuri.in" , "r" ) , *g = fopen ( "heapuri.out" , "w" );
const int MAX = 200005;
int nrReq , type , father , aux , value , Size , heap [ MAX ] , indexed [ MAX ] , i , pos , indexed_1 [ MAX ];
void heapify ( int index , int i )
{
father = index / 2;
while ( heap [ index ] < heap [ father ] && father )
{
aux = heap [ index ] , heap [ index ] = heap [ father ] , heap [ father ] = aux;
indexed [ indexed_1 [ father ] ] = index;
index /= 2 , father = index / 2;
}
indexed [ i ] = index;
indexed_1 [ index ] = i;
}
void add()
{
//get node value
fscanf ( f , "%d" , &value );
//insert it into the heap along with its index
heap [ ++Size ] = value;
heapify ( Size , Size );
}
void remove()
{
fscanf ( f , "%d" , &pos );
//delete pos-th element
heap [ indexed [ pos ] ] = heap [ Size ] , Size --;
heapify ( Size , pos );
}
void printMin()
{
fprintf ( g , "%d\n" , heap [ 1 ] );
}
void read()
{
fscanf ( f , "%d" , &nrReq );
for ( i = 1 ; i <= nrReq ; i ++ )
{
//get the type of request
fscanf ( f , "%d" , &type );
if ( type == 1 )
add();
else
if ( type == 2 )
remove();
else
printMin();
}
}
int main()
{
read();
return 0;
}