Pagini recente » Cod sursa (job #3358436) | Cod sursa (job #2809436)
#include <stdio.h> ///Disclaimer: Solutia vietii nu merge, e ceva problema la downHeap
#include <stdlib.h> ///Puteti observa ca m am zbatut pe aici putin, chiar daca n a iesit
#define LMAX 200 /// Se pare (socant!) ca nu e o idee buna sa amani tema la info pana in ultima zi
int heap[LMAX],hs,p[LMAX];
static inline int parent(int i) {return (i - 1) / 2;}
static inline int leftChild(int i) {return i * 2 + 1;}
static inline int rightChild(int i) {return i * 2 + 2;}
void climbHeap(int x){
if(x == 0)
return;
if( heap[parent(x)] > heap[x] ){
int aux = heap [parent(x)];
heap [parent(x)] = heap [x];
heap [x] = aux;
aux = p[p[x]];
p[p[x]] = p[p[parent(x)]];
p[p[parent(x)]] = aux;
climbHeap(parent(x));
}
}
void downHeap(int x){ ///WHY MAN WHY
if(x >= hs)
return;
if( heap[leftChild(x)] <= heap[rightChild(x)] && heap[leftChild(x)] < heap[x]){
int aux = heap [leftChild(x)];
heap [leftChild(x)] = heap [x];
heap [x] = aux;
aux = p[p[x]];
p[p[x]] = p[p[leftChild(x)]];
p[p[leftChild(x)]] = aux;
downHeap(leftChild(x));
} else if( heap[leftChild(x)] > heap[rightChild(x)] && heap[rightChild(x)] < heap[x]){
int aux = heap [rightChild(x)];
heap [rightChild(x)] = heap [x];
heap [x] = aux;
aux = p[p[x]];
p[p[x]] = p[p[rightChild(x)]];
p[p[rightChild(x)]] = aux;
downHeap(rightChild(x));
}
}
void deleteElement(int x){
heap[x] = heap[hs-1];
p[p[x]] = p[p[hs-1]];
hs--;
downHeap(x);
}
void add(int x, int i){
heap[hs] = x;
hs++;
p[i] = hs - 1;
climbHeap(hs-1);
}
int main(){
int n,i,t,x,j;
FILE *fin, *fout;
fin = fopen("heap.in","r");
fscanf(fin,"%d",&n);
fout = fopen("heap.out","w");
j = 0;
for(i=0;i<n;i++){
fscanf(fin,"%d",&t);
if(t == 1){
fscanf(fin,"%d",&x);
add(x,j);
j++;
} else if(t == 2){
fscanf(fin,"%d",&x);
deleteElement(p[x-1]);
} else
fprintf(fout,"%d\n",heap[0]);
for(int ii = 0;ii<hs;ii++){
printf("%d ",p[ii]);
}
printf("\n");
}
fclose(fin);
fclose(fout);
return 0;
}
//#include <stdio.h>
//#include <stdlib.h>
//#define LMAX 200
//
//int heap[LMAX],hs,p[LMAX];
//
//static inline int parent(int i) {return (i - 1) / 2;}
//static inline int leftChild(int i) {return i * 2 + 1;}
//static inline int rightChild(int i) {return i * 2 + 2;}
//
//void climbHeap(int x){
// if(x == 0)
// return;
//
// if( heap[parent(x)] > heap[x] ){
// int aux = heap [parent(x)];
// heap [parent(x)] = heap [x];
// heap [x] = aux;
//
// aux = p[x];
// p[x] = p[parent(x)];
// p[parent(x)] = aux;
//
// climbHeap(parent(x));
// }
//}
//
//void downHeap(int x){
// if(x >= hs)
// return;
//
// if( heap[leftChild(x)] <= heap[rightChild(x)] && heap[leftChild(x)] < heap[x]){
// int aux = heap [leftChild(x)];
// heap [leftChild(x)] = heap [x];
// heap [x] = aux;
//
// aux = p[x];
// p[x] = p[leftChild(x)];
// p[leftChild(x)] = aux;
//
//
// downHeap(leftChild(x));
//
// } else if( heap[leftChild(x)] > heap[rightChild(x)] && heap[rightChild(x)] < heap[x]){
// int aux = heap [rightChild(x)];
// heap [rightChild(x)] = heap [x];
// heap [x] = aux;
//
// aux = p[x];
// p[x] = p[rightChild(x)];
// p[rightChild(x)] = aux;
//
//
// downHeap(rightChild(x));
// }
//
//}
//
//void deleteElement(int x){
// heap[x] = heap[hs-1];
// p[x] = p[hs-1];
// hs--;
//
// downHeap(x);
//}
//
//void add(int x, int i){
// heap[hs] = x;
// hs++;
//
// p[i] = hs - 1;
// climbHeap(hs-1);
//}
//
//int main(){
// int n,i,t,x,j;
// FILE *fin, *fout;
//
// fin = fopen("heap.in","r");
// fscanf(fin,"%d",&n);
//
// fout = fopen("heap.out","w");
// j = 0;
// for(i=0;i<n;i++){
// fscanf(fin,"%d",&t);
//
// if(t == 1){
// fscanf(fin,"%d",&x);
// add(x,j);
// j++;
//
// } else if(t == 2){
// fscanf(fin,"%d",&x);
// deleteElement(p[x]);
//
// } else
// fprintf(fout,"%d\n",heap[0]);
//
// for(int ii = 0;ii<hs;ii++){
// printf("%d ",p[ii]);
// }
// printf("\n");
// }
// fclose(fin);
// fclose(fout);
// return 0;
//}