Pagini recente » Cod sursa (job #888126) | Cod sursa (job #1581836) | Cod sursa (job #1207276) | Cod sursa (job #2835649) | Cod sursa (job #1182613)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
void pushUp (int heap[], int &poz) {
if (poz == 0)
return;
int indice = (poz - 1) / 2;
if (heap[indice] > heap[poz]) {
int aux;
aux = heap[indice];
heap[indice] = heap[poz];
heap[poz] = aux;
pushUp(heap,indice);
}
}
void pushDown (int heap[], int &poz, int &dim) {
int indice;
if (poz * 2 + 1 >= dim)
return;
else {
if (poz * 2 + 1 < dim && poz * 2 + 2 < dim) {
if (heap[poz * 2 + 2] < heap[poz * 2 + 1])
indice = poz * 2 + 2;
else
indice = poz * 2 + 1;
}
else {
indice = poz * 2 + 1;
}
}
if (heap[poz] > heap[indice]) {
int aux;
aux = heap[indice];
heap[indice] = heap[poz];
heap[poz] = aux;
pushDown(heap,poz,dim);
}
}
int main() {
vector<int> numbers;
ifstream in;
in.open("heapuri.in");
ofstream out;
out.open("heapuri.out");
int n, i, j, Op, x, *heap, dim = 0;
in >> n;
numbers.reserve(n);
heap = new int[n];
for (i = 0; i < n; i++) {
in >> Op;
if (Op == 1) {
in >> x;
heap[dim] = x;
pushUp(heap,dim);
dim++;
numbers.push_back(x);
}
else if (Op == 2) {
in >> x;
for (j = 0; j < dim; j++)
if (heap[j] == numbers[x - 1]) {
dim--;
heap[j] = heap[dim];
pushDown(heap,j,dim);
break;
}
}
else {
out << heap[0] << "\n";
}
}
in.close();
out.close();
return 0;
}