Pagini recente » Cod sursa (job #1637024) | Cod sursa (job #2339419) | Cod sursa (job #1413730) | Cod sursa (job #2195530) | Cod sursa (job #2733414)
#include <fstream>
#include <bits/stdc++.h>
#include <cstring>
#include <stack>
#include <vector>
#include <unordered_map>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int N, op, x, L = 0, nr = 0;
int H[200010], P[200010];
void urca(int poz)
{
if (poz > 0)
{
int tata = (poz - 1)/2;
if (H[tata] > H[poz])
{
swap(H[tata], H[poz]);
urca(tata);
}
}
}
void coboara(int poz)
{
int st = poz * 2 + 1;
int dr = poz * 2 + 2;
int small = poz;
if (st < L && H[st] < H[small])
small = st;
if (dr < L && H[dr] < H[small])
small = dr;
if (small != poz)
{
swap(H[poz], H[small]);
coboara(small);
}
}
void push(int x)
{
H[L] = x;
urca(L);
L++;
}
int peek()
{
return H[0];
}
void pop(int poz)
{
int val_pop = P[poz];
for (int i = 0; i < L; ++i)
{
if (H[i] == val_pop)
{
swap(H[i], H[L - 1]);
L --;
coboara(i);
}
}
}
int main()
{
fin >> N;
for (int i = 0; i < N; ++i)
{
fin >> op;
switch(op)
{
case 1:
{
fin >> x;
nr ++;
P[nr] = x;
push(x);
break;
}
case 2:
{
fin >> x;
pop(x);
break;
}
case 3:
{
fout << peek() << "\n";
break;
}
}
}
return 0;
}