Pagini recente » Cod sursa (job #1017694) | Cod sursa (job #2330190) | Cod sursa (job #1178674) | Cod sursa (job #203861) | Cod sursa (job #823341)
Cod sursa(job #823341)
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int a[200001],b[200001],heap[200001],n,nh;
int percolate(int k)
{
while(b[heap[k]]<b[heap[k/2]] && k>1)
{
swap(heap[k],heap[k/2]);
a[heap[k]]=k;
k/=2;
}
return k;
}
int sift(int k)
{
int son; son=2*k;
while(son<=nh && b[heap[son]]<b[heap[k]])
{
if(son+1<=nh && b[heap[son+1]]<b[heap[son]])
son=son+1;
swap(heap[son],heap[k]);
a[heap[k]]=k;
k=son;
son=2*k;
}
return k;
}
int main()
{
int nr,c,x;
f>>nr;
int i;
for(i=0;i<nr;i++)
{
f>>c;
if(c==1) {
f>>x; n++; nh++; b[n]=x;
heap[nh]=n;
a[n]=percolate(nh);
}
if(c==2) { f>>x; b[x]=-1;
a[x]=percolate(a[x]);
int k=1;
heap[k]=heap[nh];
a[heap[nh]]=k;
nh--;
a[heap[k]]=sift(k);
}
if(c==3) g<<b[heap[1]]<<endl;
}
cout<<clock();
return 0;
}