Cod sursa(job #1343518)

Utilizator bogobatBerbece Daniel bogobat Data 15 februarie 2015 16:05:20
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,heap[99999],nr;
int pozitie[99999];
int valori[99999];

void sift(int N,int k){
  int son;
  while(true){

    son = 0;
    if(k*2<=N) son=k*2;
    else if(k*2+1 <=N && valori[heap[k*2]]>valori[heap[k*2+1]]) son = k*2+1;
    else return;
    swap(heap[k],heap[son]);
    pozitie[heap[k]]=k;
    k=son;

  }


}
void percolate(int k){

     int val = valori[heap[k]];
    while(valori[heap[k/2]]>val ){
        swap(heap[k],heap[k/2]);
        pozitie[heap[k]]=k;
        k=k/2;
    }
    pozitie[heap[k]]=k;


}

void inserare(){

   int x;
   f>>x;n++;
   heap[n]=n;
   pozitie[n]=n;
   valori[n]=x;
   percolate(n);

}

void eliminare(){

  int x;
  f>>x;
  valori[heap[pozitie[x]]] = 1000000003;
  sift(n,pozitie[x]);

}
int main()
{
    int x;
    int op;
    f>>nr;
    for(int i=1;i<=nr;i++){

        f>>op;
        if(op==1) inserare();
        else if(op == 2) eliminare();
        else g<<valori[heap[1]]<<endl;
    }
    return 0;
}