Pagini recente » Cod sursa (job #819816) | Borderou de evaluare (job #1567986) | Cod sursa (job #2739391) | Cod sursa (job #905335) | Cod sursa (job #2513227)
#include <bits/stdc++.h>
#define NMAX 200001
using namespace std;
//ifstream f ("heapuri.in");
//ofstream g ("heapuri.out");
int heap[NMAX],n,m,v[NMAX];
void promovare(int nod)
{
if(nod > 1)
if(heap[nod] < heap[nod/2])
{
swap(heap[nod],heap[nod/2]);
promovare(nod/2);
}
}
int indice_min(int nod)
{
if(nod*2+1 > n)return nod*2;
else
if(heap[nod*2] <= heap[nod*2+1])return nod*2;
else return nod*2+1;
}
void cernere(int nod)
{
if(nod && nod <= n/2)
{
int im = indice_min(nod);
if(heap[nod] > heap[im])
{
swap(heap[nod],heap[im]);
cernere(im);
}
}
}
void delete_xth_element(int x)
{
for(int i = 1;i<=n;++i)
if(heap[i] == v[x])
{
swap(heap[i],heap[n]);
n--;
cernere(i);
break;
}
}
void citire()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int tip,x,nrop;
scanf("%d",&nrop);
while(nrop--)
{
scanf("%d",&tip);
// se insereaza
if(tip == 1)
{
scanf("%d",&x);
heap[++n] = x;
v[++m] = x;
promovare(n);
}
else
// se stege elementul intrat al x lea
if(tip == 2)
{
scanf("%d",&x);
delete_xth_element(x);
}
else
// se afiseaza minimul
printf("%d\n",heap[1]);
}
}
int main()
{
citire();
return 0;
}