Cod sursa(job #1317229)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 14 ianuarie 2015 18:56:48
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.78 kb
#include <fstream>

using namespace std;

int parent(int i);
int leftSon(int i);
int rightSon(int i);
void heapInsert(int x, int &heapK, int heap[], int &orderK, int order[], int heapPos[]);
void siftUp(int i, int heap[], int heapPos[]);
void siftDown(int i, int N, int heap[], int heapPos[]);
void heapDelete(int x, int &heapK, int heap[], int heapPos[], int order[]);

int main()
{
    int N, i, code, x;
    ifstream f("heapuri.in");
    f >> N;

    ofstream g("heapuri.out");
    int heap[N], order[N], heapPos[N];
    int heapK = -1, orderK = -1;
    for (i = 1; i <= N; i++)
    {
        f >> code;
        if (code == 1)
        {
            f >> x;
            heapInsert(x, heapK, heap, orderK, order, heapPos);
        }
        else if (code == 2)
        {
            f >> x;
            x--;
            heapDelete(x, heapK, heap, heapPos, order);
        }
        else if (code == 3)
            g << heap[0] << "\n";
    }

    f.close();
    g.close();
    return 0;
}

int parent(int i)
{
    return (i - 1) / 2;
}

int leftSon(int i)
{
    return i * 2 + 1;
}

int rightSon(int i)
{
    return i * 2 + 2;
}

void heapInsert(int x, int &heapK, int heap[], int &orderK, int order[], int heapPos[])
{
    heap[++heapK] = x;
    order[++orderK] = x;
    heapPos[orderK] = heapK;
    siftUp(heapK, heap, heapPos);
}

void siftUp(int i, int heap[], int heapPos[])
{
    if (i)
    {
        if (heap[parent(i)] > heap[i])
        {
            heapPos[heap[i]] = parent(i);
            heapPos[heap[parent(i)]] = i;
            swap(heap[parent(i)], heap[i]);
            siftUp(parent(i), heap, heapPos);
        }
    }
}

void siftDown(int i, int N, int heap[], int heapPos[])
{
    if (leftSon(i) <= N && rightSon(i) <= N)
    {
        int m = (heap[leftSon(i)] <= heap[rightSon(i)]) ? leftSon(i) : rightSon(i);
        if (heap[i] > heap[m])
        {
            heapPos[heap[i]] = m;
            heapPos[heap[m]] = i;
            swap(heap[i], heap[m]);
            siftDown(m, N, heap, heapPos);
        }
    }
    else if (leftSon(i) <= N)
    {
        if (heap[i] > heap[leftSon(i)])
        {
            heapPos[heap[i]] = leftSon(i);
            heapPos[heap[leftSon(i)]] = i;
            swap(heap[i], heap[leftSon(i)]);
            siftDown(leftSon(i), N, heap, heapPos);
        }
    }
    else if (rightSon(i) <= N)
    {
        if (heap[i] > heap[rightSon(i)])
        {
            heapPos[heap[i]] = rightSon(i);
            heapPos[heap[rightSon(i)]] = i;
            swap(heap[i], heap[rightSon(i)]);
            siftDown(rightSon(i), N, heap, heapPos);
        }
    }
}

void heapDelete(int x, int &heapK, int heap[], int heapPos[], int order[])
{
    heapPos[heap[heapK]] = heapPos[order[x]];
    heap[heapPos[order[x]]] = heap[heapK--];
    siftDown(heapPos[order[x]], heapK, heap, heapPos);
}