Pagini recente » Cod sursa (job #1299484) | Cod sursa (job #1316447) | Profil mariaman | Cod sursa (job #1410184) | Cod sursa (job #2890334)
#include <fstream>
using namespace std;
const int dim = 400003;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int v[dim]; //reprezinta Heap-ul
int ordine[200003], o = 0;
void swap(int a, int b) {
int aux = v[a];
v[a] = v[b];
v[b] = aux;
}
void goUp(int v[], int dim, int nod) {
int key = v[nod];
while( nod > 1 && key < v[ nod / 2 ] ) {
v[nod] = v[ nod / 2 ];
nod /= 2;
}
v[nod] = key;
}
void goDown(int v[], int dim, int nod) {
int son;
do {
son = 0;
if( nod * 2 <= dim ) {
son = nod * 2;
if( nod * 2 + 1 <= dim && v[nod * 2 + 1] < v[son] )
son = nod * 2 + 1;
if( v[son] >= v[nod] )
son = 0;
}
if( son ) {
swap(son, nod);
nod = son;
}
} while( son );
}
int main() {
int n, op, nod, dim = 0, poz;
fin >> n;
for(int i = 0; i < n; i++) {
fin >> op;
if( op != 3 ) fin >> nod;
if( op == 1 ) {
//inseram
v[++dim] = nod;
ordine[++o] = nod;
goUp(v, dim, dim);
} else if( op == 2 ) {
//stergem
for(int j = 1; j <= dim; j++)
if( v[j] == ordine[nod] ) {
poz = j;
break;
}
v[ poz ] = v[dim--];
if( nod > 1 && v[ poz ] < v[ poz / 2] )
goUp(v, dim, poz);
else {
goDown(v, dim, poz);
}
} else {
//afisam minimul
fout << v[1] << '\n';
}
}
return 0;
}