Cod sursa(job #2735896)

Utilizator BAlexandruBorgovan Alexandru BAlexandru Data 2 aprilie 2021 22:23:51
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.88 kb
#include <fstream>

using namespace std;

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

class minHeap
{
    private:
        struct heapElement
        {
            int key;
            int value;
            heapElement();
            heapElement(const int _KEY, const int _VALUE);
        };

        int heapSize;
        int capacity;

        int* pos;
        heapElement* harr;

    public:
        minHeap();
        minHeap(const int _CAPACITY, const int _MAXKEY);
        ~minHeap();

        int parent(const int node) {return node / 2;}
        int leftChild(const int node) {return node * 2;}
        int rightChild(const int node) {return node * 2 + 1;}

        void heapifyUp(int node);
        void heapifyDown(int node);

        int getMin();
        void extractMin();

        void insertElement(const int key, const int value);
        void deleteElement(const int key);
};

minHeap :: heapElement :: heapElement()
{
    key = 0;
    value = 0;
}

minHeap :: heapElement :: heapElement(const int _KEY, const int _VALUE)
{
    key = _KEY;
    value = _VALUE;
}

minHeap :: minHeap()
{
    heapSize = 0;
    capacity = 0;

    pos = NULL;
    harr = NULL;
}

minHeap :: minHeap(const int _CAPACITY, const int _MAXKEY)
{
    heapSize = 0;
    capacity = _CAPACITY;

    pos = new int[_MAXKEY + 1];
    harr = new heapElement[_CAPACITY + 1];
}

minHeap :: ~minHeap()
{
    delete[] pos;
    delete[] harr;
}

void minHeap :: heapifyUp(int node)
{
    while (node > 1 && harr[node].value < harr[parent(node)].value)
    {
        swap(pos[harr[node].key], pos[harr[parent(node)].key]);
        swap(harr[node], harr[parent(node)]);
        node = parent(node);
    }
}

void minHeap :: heapifyDown(int node)
{
    int child = 0;
    do
    {
        child = 0;

        if (leftChild(node) <= heapSize)
        {
            child = leftChild(node);

            if (rightChild(node) <= heapSize && harr[rightChild(node)].value < harr[leftChild(node)].value)
                child = rightChild(node);

            if (harr[child].value >= harr[node].value)
                child = 0;
        }

        if (child)
        {
            swap(pos[harr[node].key], pos[harr[child].key]);
            swap(harr[node], harr[child]);
            node = child;
        }

    }while (child);
}

int minHeap :: getMin()
{
    return harr[1].value;
}

void minHeap :: extractMin()
{
    if (!heapSize)
        return;

    if (heapSize == 1)
    {
        pos[harr[1].key] = 0;
        heapSize--;
        return;
    }

    pos[harr[1].key] = 0;
    harr[1] = harr[heapSize];
    pos[harr[1].key] = 1;
    heapSize--;

    heapifyDown(1);
}

void minHeap :: insertElement(const int key, const int value)
{
    if (heapSize == capacity)
        return;

    heapSize++;
    harr[heapSize] = heapElement(key, value);
    pos[key] = heapSize;

    heapifyUp(heapSize);
}

void minHeap :: deleteElement(const int key)
{
    if (!pos[key])
        return;

    int p = pos[key];

    if (p == heapSize)
    {
        pos[key] = 0;
        heapSize--;
        return;
    }

    pos[key] = 0;
    harr[p] = harr[heapSize];
    pos[harr[p].key] = p;
    heapSize--;

    if (harr[p].value >= harr[parent(p)].value)
        heapifyDown(p);
    else
        heapifyUp(p);
}

int main()
{
    int n; f >> n;

    minHeap h(n, n);
    int key = 0;

    while (n--)
    {
        int type; f >> type;

        if (type == 1)
        {
            int x; f >> x;
            key++; h.insertElement(key, x);
        }
        else if (type == 2)
        {
            int x; f >> x;
            h.deleteElement(x);
        }
        else if (type == 3)
        {
            g << h.getMin() << "\n";
            //h.extractMin();
        }
    }

    return 0;
}