Cod sursa(job #995112)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 7 septembrie 2013 15:38:48
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int N;
int Heap[200002],Value[200002];
int NHeap,Poz[200002];
bool Use[200002];
void Swap(int X, int Y)
{
    swap(Heap[X],Heap[Y]);
    swap(Poz[Heap[X]],Poz[Heap[Y]]);
}
void Percolate (int X)
{
    int Father=(X>>1);
    if (Father>0 && Value[Heap[X]]<Value[Heap[Father]])
    {
        Swap (X,Father);
        Percolate (Father);
    }
}
void Sift(int X)
{
    int Son=(X<<1);
    if(Son+1<=NHeap && Value[Heap[Son+1]]<Value[Heap[Son]])
      Son++;
    if(Son<=NHeap && Value[Heap[Son]]<Value[Heap[X]])
        {
            Swap(Son,X);
            Sift(Son);
        }
}
void Insert(int X)
{
    Heap[++NHeap]=X;
    Poz[X]=NHeap;
    Percolate(NHeap);
}
void Erase(int X)
{
    int Father=(X>>1);
    Swap(X,NHeap);
    --NHeap;
    if ((X > 1) && (Heap[X] > Heap[Father]))
        Percolate(X);
    else
        Sift(X);
}
void Erase_First()
{
    Heap[1] = Heap[NHeap--];
    Sift(1);
}
void Solve_Heap()
{
    int i,number=0,introduce=0;
    for(i=1;i<=N;i++)
    {
        int type,value;
        f>>type;
        if(type==1)
        {
            f>>value;
            Value[++introduce]=value;
            Insert(introduce);

        }
        if(type==2)
        {
            f>>value;
            if(Poz[value]!=1)
                Erase(Poz[value]);
            else
               Erase_First();
        }
        if(type==3)
            g<<Value[Heap[1]]<<"\n";
    }
}
int main()
{
    f>>N;
    Solve_Heap();
    return 0;
}