Cod sursa(job #733571)

Utilizator horeste12Stoianovici Horatiu Andrei horeste12 Data 12 aprilie 2012 16:10:04
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.85 kb
#include <fstream>
#include <vector>
using namespace std;
///clasa Heap: push,empty,size,pop(pop si returneaza primul element), la declarare poti pune marimea
int Less(int a, int b)
{
    return a<b;
}
class Heap
{
private:
    int SIZE,MAX_SIZE;
    vector<int> heap;
    int (*comp)(int,int);
    void sift(int);
    void percolate(int);
public:
    void push(int element)
    {
        ++SIZE;
        heap.push_back(element);
        percolate(SIZE-1);
    }
    int pop()
    {
        int ret=heap[0];
        heap[0]=heap[SIZE-1];
        --SIZE;
        sift(0);
        return ret;
    }
    bool empty()
    {
        return heap.empty();
    }
    int size()
    {
        return SIZE;
    }
    int front()
    {
        return heap[0];
    }
    void erase(int x)
    {
        for(int i=0;i<heap.size();i++)
            if(heap[i]==x)
            {
                heap[i]=heap[SIZE-1];
                --SIZE;
                percolate(i);
            }
    }
    Heap()
    {
        SIZE=0;
        comp=&Less;
        MAX_SIZE=-1;
    }
    Heap(int x)
    {
        heap.reserve(x+1);
        SIZE=0;
        comp=&Less;
        MAX_SIZE=x;
    }
    Heap(int (*f)(int, int))
    {
        comp=f;
        SIZE=0;
        MAX_SIZE=-1;
    }
    Heap(int x,int (*f)(int, int))
    {
        comp=f;
        SIZE=0;
        MAX_SIZE=x;
        heap.reserve(x+1);
    }
    ~Heap()
    {
        heap.erase(heap.begin(),heap.end());
    }
};

void Heap::sift(int i)
{
    int ok=1;
    while(ok)
    {
        if(i*2+1<SIZE&&(*comp)(heap[i*2+1],heap[i]))
        {
            if(i*2+2<SIZE&&(*comp)(heap[i*2+2],heap[i]))
            {
                int aux=(*comp)(heap[i*2+1],heap[i*2+2])?i*2+1:i*2+2;
                swap(heap[aux],heap[i]);
                i=aux;
            }
            else
            {
                swap(heap[i*2+1],heap[i]);
                i=i*2+1;
            }
        }
        else
            if(i*2+2<SIZE&&(*comp)(heap[i*2+2],heap[i]))
            {
                swap(heap[i*2+2],heap[i]);
                i=i*2+2;
            }
            else
                ok=0;
    }
}

void Heap::percolate(int i)
{
    int ok=1;
    while(ok)
    {
        if(i>0&&!(*comp)(heap[(i-1)/2],heap[i]))
        {
            swap(heap[i],heap[(i-1)/2]);
            i=(i-1)/2;
        }
        else
            ok=0;
    }
    sift(i);
}

int n,d[200010];

int comp(int a, int b)
{
    return d[a]<d[b];
}

ifstream f("heapuri.in");
ofstream g("heapuri.out");

Heap H(comp);


int main()
{
    int N,dim=0;
    f>>N;
    while(N--)
    {
        int opt,a;
        f>>opt;
        if(opt==1)
        {
            f>>a;
            d[++dim]=a;
            H.push(dim);
        }
        else
            if(opt==2)
            {
                f>>a;
                H.erase(a);
            }
            else
                g<<d[H.front()]<<'\n';
    }
    return 0;
}