Pagini recente » Cod sursa (job #1897776) | Cod sursa (job #20126) | Cod sursa (job #1299996) | Cod sursa (job #1187730) | Cod sursa (job #2742472)
#include <vector>
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
void urca( vector <int> &heap, int pozitie ){
int ok = 1, x;
while( ok != 0 && pozitie > 0 ){
if( heap[(pozitie-1)/2] > heap[pozitie] ){
x = heap[pozitie];
heap[pozitie] = heap[(pozitie-1)/2];
heap[(pozitie-1)/2] = x;
pozitie = (pozitie - 1)/2;
}
else ok = 0;
}
}
void coboara( vector <int> &heap, int pozitie ){
int ok = 1, x;
while( pozitie < heap.size() && ok != 0 ){
if( heap[pozitie] > heap[ pozitie*2 + 1 ] && (pozitie*2+1) < heap.size() ){
x = heap[pozitie];
heap[pozitie] = heap[pozitie*2+1];
heap[pozitie*2+1] = x;
pozitie = pozitie * 2 + 1;
}
else ok = 0;
}
}
int cautare( vector <int> heap, int numar ){
int ok = 1;
for( int i = 0; i < heap.size() && ok == 1 ; i++ ){
if( heap[i] == numar ){
ok = 0;
return i;
}
}
}
int adauga_element( vector <int> &heap, int numar ){
heap.push_back(numar);
urca( heap, heap.size()-1 );
}
int elimina_element( vector <int> &heap, int numar ){
int i = cautare( heap, numar );
heap[i] = heap[ heap.size() - 1 ];
heap.pop_back();
coboara( heap, i );
}
int main(){
vector <int> heap;
vector <int> numere;
ifstream fin( "heapuri.in" );
ofstream fout( "heapuri.out" );
int n;
fin >> n;
for( int i = 0; i < n; i++ ){
int operatie;
fin >> operatie;
if( operatie == 1 ){
int numar;
fin >> numar;
numere.push_back(numar);
adauga_element( heap, numar );
}
if( operatie == 2 ){
int pozitie;
fin >> pozitie;
elimina_element( heap, numere[pozitie-1] );
}
if( operatie == 3 ){
fout << heap[0] << endl;
}
}
}