Cod sursa(job #805625)

Utilizator radu_bucurRadu Bucur radu_bucur Data 31 octombrie 2012 20:08:49
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int h[100], v[100], poz[100],i,n,x,y,nh,k;
void schimba(int x, int y)
{
    int aux;
    aux=0;
    aux=h[x]; h[x]=h[y]; h[y]=aux;
    poz[h[x]]=x;
    poz[h[y]]=y;

}
void urc(int p)
{
    if (p==1||v[h[p/2]]<v[h[p]]) return;
    schimba(p/2,p);
    urc(p/2);
}
void cobor(int p)
{
    int fs=p*2,fd=p*2+1,max1=p,aux1;
    if(v[h[fs]]<v[h[max1]]&&fs<=nh) max1=fs;
    if(v[h[fd]]<v[h[max1]]&&fd<=nh) max1=fd;
    if (max1!=p)
    {
        schimba(p,max1);
        cobor(p);
    }
    urc(p);

}
int main()
{

  in>>n; nh=0; k=0;
  for (i=1;i<=n;i++)
  {
     in>>x;
     if (x==1)
     {
        in>>v[++k];
        h[++nh]=k;
        poz[k]=nh;
        urc(nh);
     }
     if(x==2)
     {
        in>>y;
        h[poz[y]]=h[nh];
        nh--;
        cobor(poz[y]);
     }
     if(x==3) out<<v[h[1]]<<"\n";
  }
    return 0;
}