Cod sursa(job #1990488)

Utilizator b10nd3Oana Mancu b10nd3 Data 12 iunie 2017 02:24:43
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include<fstream>
using namespace std;

void swap(int heap[], int where1, int where2){
   int aux=heap[where1]; heap[where1]=heap[where2]; heap[where2]=aux;
}

void insert(int heap[],int noEl){
   int x=noEl; 
   while(heap[x]<=heap[x/2] && x>1){
      swap(heap,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(int heap[], int &noEl, int where){
     int changeWith;
     heap[where]=heap[noEl]; noEl--;
     while((heap[where]>heap[where*2] && where*2<=noEl) || (heap[where]>heap[where*2+1] && where*2+1<=noEl)){
         if(where*2+1<=noEl) changeWith=minWhere(heap[where*2],heap[where*2+1],where);
         else changeWith=where*2;
         swap(heap,where,changeWith);
         where=changeWith;
     }
}

void printHeap(int heap[], int noEl, ofstream out){
   for(int i=1;i<=noEl;i++) out<<heap[i]<<" ";
   out<<endl;
}

int find(int heap[], int noEl, int who){
   for (int i=1;i<=noEl;i++) if(who==heap[i]) return i;
}

int main(){
int x,a,i,n,noEl=0,c=0,where;
ifstream in("heapuri.in"); ofstream out("heapuri.out");
in>>n; int heap[n+1], cron[n+1]; 
for(i=1;i<=n;i++){
  in>>a;
  if(a==1){
     in>>x; 
     noEl++; heap[noEl]=x; 
     c++; cron[c]=x;
     insert(heap,noEl);
  }
  if(a==2){
    in>>x; 
    where=find(heap,noEl,cron[x]);
    erase(heap,noEl,where);  
  }
  if(a==3){
    out<<heap[1]<<"\n";
  }
}


in.close(); out.close();
return 0;
}