Pagini recente » Cod sursa (job #1233741) | Cod sursa (job #1542237) | Cod sursa (job #1200218) | Cod sursa (job #1142997) | Cod sursa (job #2910714)
#include <fstream>
using namespace std;
ifstream cin ("heapuri.in");
ofstream cout ("heapuri.out");
struct Pozitie
{
int first, second;
};
Pozitie Poz[100];
int Heap[100], N = 0, Nr;
int Peek()
{
return Poz[Heap[1]].first;
}
bool Compare(int a, int b)
{
return Poz[Heap[a]].first < Poz[Heap[b]].first;
}
void Swap(int a, int b)
{
swap(Heap[a], Heap[b]);
swap(Poz[Heap[a]].second, Poz[Heap[b]].second);
}
void UpHeap(int x)
{
int Father = x / 2;
if(Father && Compare(x, Father))
Swap(x, Father), UpHeap(Father);
}
void DownHeap(int x)
{
int Son = 2 * x;
if(Son + 1 <= N && Compare(Son + 1, Son))
Son ++;
if(Son <= N)
Swap(Son, x), DownHeap(Son);
}
void Insert(int x)
{
N ++;
Heap[N] = N;
Poz[N].first = x;
Poz[N].second = N;
UpHeap(N);
}
void Erase(int x)
{
int y = Poz[x].second;
Swap(Poz[x].second, N);
N--;
UpHeap(y);
DownHeap(y);
}
void print()
{
for(int i = 1; i <= N; ++i)
cout << Poz[Heap[i]].first << " ";
}
int main()
{
cin >> Nr;
for(int i = 1; i <= Nr; ++i)
{
int number, value;
cin >> number;
if(number == 3) cout << Peek() << endl;
else{
cin >> value;
if(number == 1) Insert(value);
else Erase(value);
}
}
return 0;
}