Pagini recente » Cod sursa (job #1695069) | Cod sursa (job #3253909) | Cod sursa (job #3292439) | Cod sursa (job #2371364) | Cod sursa (job #1237260)
#include <stdlib.h>
#include <fstream>
#define HEAP_MAX_SIZE 200005
using namespace std;
typedef struct
heap
{
int heapToIndex[ HEAP_MAX_SIZE ], indexToHeap[ HEAP_MAX_SIZE ], values[ HEAP_MAX_SIZE ];
int size, valuesSize;
heap( )
{
size = 0;
valuesSize = 0;
}
} heap;
void percolate( heap &myHeap, int heapIndex )
{
int auxValue = myHeap.values[ myHeap.heapToIndex[ heapIndex ] ], myIndex = myHeap.heapToIndex[ heapIndex ];
while( heapIndex > 0 && auxValue < myHeap.values[ myHeap.heapToIndex[ heapIndex / 2 ] ] )
{
myHeap.heapToIndex[ heapIndex ] = myHeap.heapToIndex[ heapIndex / 2 ];
myHeap.indexToHeap[ myHeap.heapToIndex[ heapIndex / 2 ] ] = heapIndex;
heapIndex /= 2;
}
myHeap.heapToIndex[ heapIndex ] = myIndex;
myHeap.indexToHeap[ myIndex ] = heapIndex;
}
void insert( heap &myHeap, int value )
{
myHeap.values[ myHeap.valuesSize ] = value;
myHeap.indexToHeap[ myHeap.valuesSize ] = myHeap.size;
myHeap.heapToIndex[ myHeap.size ] = myHeap.valuesSize;
++myHeap.valuesSize;
++myHeap.size;
percolate( myHeap, myHeap.size - 1 );
}
int leftSon( int heapIndex )
{
return 2 * heapIndex + 1;
}
int rightSon( int heapIndex )
{
return 2 * heapIndex + 2;
}
int getSon( heap myHeap, int heapIndex )
{
if( leftSon( heapIndex ) <= myHeap.size && myHeap.values[ myHeap.heapToIndex[ leftSon( heapIndex ) ] ] < myHeap.values[ myHeap.heapToIndex[ heapIndex ] ] )
return leftSon( heapIndex );
if( rightSon( heapIndex ) <= myHeap.size && myHeap.values[ myHeap.heapToIndex[ rightSon( heapIndex ) ] ] < myHeap.values[ myHeap.heapToIndex[ heapIndex ] ] )
return rightSon( heapIndex );
return -1;
}
void sift( heap &myHeap, int heapIndex )
{
int myIndex = myHeap.heapToIndex[ heapIndex ];
int son;
while( ( son = getSon( myHeap, heapIndex ) ) != -1 )
{
myHeap.heapToIndex[ heapIndex ] = myHeap.heapToIndex[ son ];
myHeap.indexToHeap[ myHeap.heapToIndex[ son ] ] = heapIndex;
heapIndex = son;
}
myHeap.heapToIndex[ heapIndex ] = myIndex;
myHeap.indexToHeap[ myIndex ] = heapIndex;
}
void remove( heap &myHeap, int valueIndex )
{
int heapIndex = myHeap.indexToHeap[ valueIndex ];
--myHeap.size;
myHeap.indexToHeap[ valueIndex ] = -1;
myHeap.heapToIndex[ heapIndex ] = myHeap.heapToIndex[ myHeap.size ];
myHeap.indexToHeap[ myHeap.heapToIndex[ myHeap.size ] ] = heapIndex;
if( myHeap.values[ myHeap.heapToIndex[ heapIndex ] ] < myHeap.values[ myHeap.heapToIndex[ heapIndex / 2 ] ] )
percolate( myHeap, heapIndex );
else
sift( myHeap, heapIndex );
}
int main( )
{
ifstream in( "heapuri.in" );
ofstream out( "heapuri.out" );
heap myHeap;
int tasksNumber, task;
in >> tasksNumber;
for( int index = 0; index < tasksNumber; ++index )
{
in >> task;
if( task == 1 )
{
int value;
in >> value;
insert( myHeap, value );
}
else
if( task == 2 )
{
int valueIndex;
in >> valueIndex;
remove( myHeap, valueIndex - 1 );
}
else
out << myHeap.values[ myHeap.heapToIndex[ 0 ] ] << "\n";
for( int i=0;i<myHeap.size;++i)
printf( "%d ", myHeap.values[ myHeap.heapToIndex[ i ] ] );
printf( "\n" );
for( int i=0;i<myHeap.valuesSize;++i)
printf( "%d ", myHeap.indexToHeap[ i ] );
printf( "\n" );
for( int i=0;i<myHeap.valuesSize;++i)
printf( "%d ", myHeap.heapToIndex[ i ] );
printf( "\n" );
printf( "\n" );
}
return EXIT_SUCCESS;
}