Cod sursa(job #719553)
Utilizator | Data | 21 martie 2012 21:10:38 | |
---|---|---|---|
Problema | Heapuri | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 2.19 kb |
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[200005],poz[200005],ordin[200005];
int m=0,a,k=0,j;
void insert(){fin>>a;
poz[++k]=++m;
heap[m]=a;
ordin[m]=k;
j=m;
while(m/2>0&&heap[m/2]>heap[m]){int aux;
aux=heap[m/2];
heap[m/2]=heap[m];
heap[m]=aux;
poz[ordin[m]]=m/2;
poz[ordin[m/2]]=m;
aux=ordin[m/2];
ordin[m/2]=ordin[m];
ordin[m]=aux;
m=m/2;}
m=j;}
void remove(){fin>>a;
heap[poz[a]]=heap[m];
heap[m]=0;
poz[ordin[m]]=poz[a];
ordin[poz[a]]=ordin[m];
j=poz[a];
m--;
while(heap[2*j]&&(heap[2*j]<heap[j]||(heap[2*j+1]&&heap[2*j+1]<heap[j]))){
int aux,pozi;
if(heap[2*j+1]&&heap[2*j+1]<heap[2*j])pozi=2*j+1;
else pozi=2*j;
aux=heap[j];
heap[j]=heap[pozi];
heap[pozi]=aux;
poz[ordin[j]]=pozi;
poz[ordin[pozi]]=j;
aux=ordin[pozi];
ordin[pozi]=ordin[j];
ordin[j]=aux;
j=pozi;}
}
int main()
{int n;
fin>>n;
int x;
for(int i=1;i<=n;i++)
{fin>>x;
if(x==1)insert();
if(x==2)remove();
if(x==3)fout<<heap[1]<<"\n";
}
return 0;
}