Cod sursa(job #1343868)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 15 februarie 2015 23:49:58
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.44 kb
/*
     P.S: tata < fiu
*/
#include  <fstream>
#define DIM 1000010
using namespace std;

ifstream fin ("heapuri.in" );
ofstream fout("heapuri.out");

int n, m, i, j, k, ok, minim, x, y;
int v[DIM], t, h, w[DIM], l, cod;
int val, aux, p;

int cond_1(){return cod == 1;}
int cond_2(){return cod == 2;}
int cond_3(){return cod == 3;}

void Build_Heap(){
     fin >> n;
     for(p = 1; p <= n; p ++){
          fin >> cod;
          /*
               Cand cod == 1;
          */
          if(cond_1()){
               fin >> val; k ++; l ++;
               v[k] = val; w[k] = l;
               /*
                    Transform nodul
                       in tata
               */
               t = k;
               while(v[t] < v[t/2] && t > 1){
                    aux = v[t]; v[t] = v[t/2]; v[t/2] = aux;
                    aux = w[t]; w[t] = w[t/2]; w[t/2] = aux;
                    t /= 2;
               }
          }
          /*
               Cand cod == 2;
          */
          if(cond_2()){
               fin >> val; i = 1;
               while(w[i] != val) i ++;
               v[i] = v[k]; w[i] = w[k];
               v[k] = 0; w[k] = 0; k --;
               /*
                    Caz 1: nod = fiu
                    => nod devine tata
               */
               t = i;
               while(v[t] < v[t/2] && t > 1){
                    aux = v[t]; v[t] = v[t/2]; v[t/2] = aux;
                    aux = w[t]; w[t] = w[t/2]; w[t/2] = aux;
                    t /= 2;
               }
               /*
                    Caz 2: nod = tata
                    => nod devine fiu
               */
               t = i;
               while(v[t] > v[t*2] && v[t*2] != 0){
                    aux = v[t]; v[t] = v[t*2]; v[t*2] = aux;
                    aux = w[t]; w[t] = w[t*2]; w[t*2] = aux;
                    t *= 2;
               }
               t = i;
               while(v[t] > v[t*2+1] && v[t*2+1] != 0){
                    aux = v[t]; v[t] = v[t*2+1]; v[t*2+1] = aux;
                    aux = v[t]; v[t] = v[t*2+1]; v[t*2+1] = aux;
                    t = t*2+1;
               }
          }
          /*
               Cand cod == 3;
          */
          if(cond_3()){
               /*
                    Il afisez pe tata
               */
               fout << v[1] << "\n";
          }
     }
     return;
}

int main(){
     Build_Heap();
     return 0;
}