Pagini recente » Cod sursa (job #755589) | Cod sursa (job #2023766) | Cod sursa (job #3172079) | Cod sursa (job #883996) | Cod sursa (job #1990708)
#include<fstream>
using namespace std;
struct node{
int value, cronOrder;
node(int _value=0, int _cronOrder=0){
value=_value; cronOrder=_cronOrder;
}
};
void swap(node heap[],int cronWhere[], int where1, int where2){
node aux=heap[where1]; heap[where1]=heap[where2]; heap[where2]=aux;
cronWhere[heap[where1].cronOrder]=where1;
cronWhere[heap[where2].cronOrder]=where2;
}
void insert(node heap[],int noEl, int cronWhere[]){
int x=noEl;
while(heap[x].value<=heap[x/2].value && x>1){
swap(heap,cronWhere, x,x/2);
x=x/2;
}
}
int minWhere(int x, int y,int where){
if(x<y) return where*2;
return where*2+1;
}
void erase(node heap[], int &noEl, int where, int cronWhere[]){
int changeWith;
heap[where]=heap[noEl]; noEl--;
while((heap[where].value>heap[where*2].value && where*2<=noEl) || (heap[where].value>heap[where*2+1].value && where*2+1<=noEl)){
if(where*2+1<=noEl) changeWith=minWhere(heap[where*2].value,heap[where*2+1].value,where);
else changeWith=where*2;
swap(heap,cronWhere,where,changeWith);
where=changeWith;
}
}
void printHeap(node heap[], int noEl, ofstream out){
for(int i=1;i<=noEl;i++) out<<"("<<heap[i].value<<","<<heap[i].cronOrder<<") ";
out<<endl;
}
int main(){
int x,a,i,n,noEl=0,c=0,where;
ifstream in("heapuri.in"); ofstream out("heapuri.out");
in>>n; node heap[n+1]; int cronWhere[n+1];
for(i=1;i<=n;i++){
in>>a;
if(a==1){
in>>x;
noEl++; heap[noEl].value=x;
c++; heap[noEl].cronOrder=c;
cronWhere[c]=noEl;
insert(heap,noEl,cronWhere);
}
if(a==2){
in>>x;
where=cronWhere[x];
erase(heap,noEl,where,cronWhere);
}
if(a==3){
out<<heap[1].value<<'\n';
}
}
in.close(); out.close();
return 0;
}