Pagini recente » Cod sursa (job #1533576) | Cod sursa (job #555200) | Cod sursa (job #2500592) | Cod sursa (job #2689666) | Cod sursa (job #398602)
Cod sursa(job #398602)
#include <stdio.h>
#include <algorithm>
#define M 200010
using namespace std;
FILE*f=fopen("heapuri.in","r");
FILE*g=fopen("heapuri.out","w");
int h[M],nrop,i,x,n,l,a[M],pos[M];
void sus(int l){
while(l>1 && a[h[l]]<a[h[l/2]]){
swap(h[l],h[l/2]);
pos[h[l]]=l;
pos[h[l/2]]=l/2;
l/=2;
}
}
void jos(int k){
int fiu,c;
do{
fiu=0;
c=k<<1;
if(c<=l){
fiu=c;
if(c+1<=l && a[h[c]]>a[h[c+1]]){
fiu=c+1;
}
if(a[h[fiu]]>a[h[k]]){
fiu=0;
}
}
if(fiu){
swap(h[fiu],h[k]);
swap(pos[h[fiu]],pos[h[k]]);
k=fiu;
}
}while(fiu);
}
int main(){
int tip;
int j,b;
fscanf(f,"%d",&nrop);
for(int i=0;i<nrop;i++){
fscanf(f,"%d ",&tip);
if(tip==1){
fscanf(f,"%d",&x);
n++,l++;
a[n]=x;
pos[n]=l;
h[l]=n;
sus(l);
}
else if (tip==2){
fscanf(f,"%d",&x);
b=pos[x];
swap(h[pos[x]],h[l]);
pos[h[pos[x]]]=pos[x];
pos[h[l]]=l;
l--;
jos(b);
}
else {
for(j=1;j<=l;j++){
printf("%d ",a[h[j]]);
}
printf("\n");
fprintf(g,"%d\n",a[h[1]]);
}
}
fclose(f);
fclose(g);
return 0;
}