Pagini recente » Cod sursa (job #1708615) | Cod sursa (job #1279354) | Cod sursa (job #2138740) | Cod sursa (job #3293584) | Cod sursa (job #3256469)
#include <iostream>
#include <fstream>
#define NMAX 200002
using namespace std;
int a[NMAX], n, b[NMAX], h[NMAX];
void coborare (int h[], int p, int n)
{
int aux, r;
while(2*p<=n)
{
r=2*p;
if(r+1<=n&&a[h[r+1]]<a[h[r]])
r=r+1;
if(a[h[r]]<a[h[p]])
{
aux=h[r]; h[r]=h[p]; h[p]=aux;
p=r;
}
else break;
}
}
void urcare (int h[], int p, int n)
{
int aux;
while(p/2>=1&&a[h[p/2]]>a[h[p]])
{
aux=h[p/2];
h[p/2]=h[p];
h[p]=aux;
p=p/2;
}
}
int main()
{ ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int i, x, k=0, m=0, comanda;
fin>>n;
for(i=1;i<=n;i++)
{
fin>>comanda;
if(comanda==1)
{
fin>>x;
k++; a[k]=x; b[k]=0;
m++; h[m]=k; urcare(h,m,m);
}
if(comanda==2)
{
fin>>x;
b[x]=1;
}
if(comanda==3)
{
while(b[h[1]]==1)
{while(b[h[m]]==1) m--;
h[1]=h[m];
m--;
coborare(h,1,m);
}
fout<<a[h[1]]<<"\n";
}
}
fin.close();
fout.close();
return 0;
}