Pagini recente » Cod sursa (job #347742) | Cod sursa (job #1831452) | Cod sursa (job #2685356) | Cod sursa (job #2685354) | Cod sursa (job #1814351)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int a[200005],b[200005],n,k;
int heap[200005];
void urcare ( int poz)
{
int aux;
if(poz/2>=1 && heap[poz]<heap[poz/2])
{
aux=heap[poz/2];
heap[poz/2]=heap[poz];
heap[poz]=aux;
urcare(poz/2);
}
}
void coborare(int poz)
{
int aux;
int r;
if(2*poz<=n)
{
r=2*poz;
if(2*poz+1<=n && heap[2*poz+1]<heap[r])
{
r=2*poz+1;
}
if(heap[poz]>heap[r])
{
aux=heap[poz];
heap[poz]=heap[r];
heap[r]=aux;
coborare(r);
}
}
}
void inserare(int x)
{
n++;
heap[n]=x;
urcare(n);
}
int i,ins,x,N;
int main()
{
fin>>N;
for(i=1;i<=N;i++)
{
fin>>ins;
if(ins==1)
{
fin>>x;
k++;
a[k]=x;
b[k]=0;
inserare(x);
}
else if(ins==2)
{
fin>>x;
b[x]=1;
}
else
{
while(b[heap[1]]==1)
{
heap[1]=heap[n];
n--;
coborare(1);
}
fout<<heap[1]<<'\n';
}
}
return 0;
}