Cod sursa(job #372314)

Utilizator bora_marianBora marian bora_marian Data 9 decembrie 2009 10:46:46
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include<fstream>
using namespace std;
int v[10000],h[10000],poz[10000],n,m;
void cerne(int q,int n);
void promoveaza(int q);
int main()
{
  int nro,o,x;
  ifstream fin("heapuri.in");
  fin>>nro;
  ofstream fout("heapuri.out");
  for(;nro;nro--)
  { 
    fin>>o;
    if(0==3)
      fout<<v[h[1]];
    if(o==1)
      {fin>>x;
       v[++m]=x;
       poz[m]=++n;
       h[n]=m;
       promoveaza(n);}
     else
         { 
           fin>>x;
           h[poz[x]]=h[n];
           n--;
           cerne(poz[x],n);
       }
    }
}       
void promoveaza(int k)
{
  int eh=0;
  while(k/2!=0 && eh==0)
  {
    if(v[h[k]]>=v[h[k/2]])
       eh=1;
    else
    {
       int aux=h[k];
         h[k]=h[k/2];
         h[k/2]=aux;
         poz[h[k]]=k;
         poz[h[k/2]]=k/2;
     }
}
}
void cerne(int k,int n)
{
  int eh=0;
  while(2*k<=n && eh==0)
     {
       int i=2*k;
       if(i+1<=n && v[h[i+1]]<v[h[i]])
            i=i+1;
       if(v[h[k]]<=v[h[i]])
           eh=1;
       else
         {
           int aux=h[k];
           h[k]=h[i];
           h[i]=aux;
           poz[h[k]]=k;
           poz[h[k/2]]=k/2;
           k=i;
         }
    }
}