Cod sursa(job #813159)

Utilizator DevilShadowJunc Raul Cosmin DevilShadow Data 14 noiembrie 2012 23:14:38
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <iostream>
#include <fstream>

using namespace std;
int n, type, Heap[200001], Pos[200001], A[200001], L, NR;
ifstream f("heapuri.in");
ofstream g("heapuri.out");

void push(int x)
{
    int aux;
    while(x / 2 && A[Heap[x]] < A[Heap[x / 2]])
    {
        aux = Heap[x];
        Heap[x] = Heap[x / 2];
        Heap[x / 2] = aux;

        Pos[Heap[x]] = x;
        Pos[Heap[x / 2]] = x / 2;
        x /= 2;
    }
}
void pop(int x)
{
    int aux, y = 0;
    while(y != x)
    {
        y = x;

        if(y * 2 <= L && A[Heap[y * 2]] < A[Heap[x]]) x = y * 2;
        if(y * 2 + 1<= L && A[Heap[y * 2 + 1]] < A[Heap[x]]) x = y * 2 + 1;

        aux = Heap[x];
        Heap[x] = Heap[y];
        Heap[y] = aux;

        Pos[Heap[x]] = x;
        Pos[Heap[y]] = y;
    }
}

int main()
{
    f >> n;
    int x;
    for(int i = 0; i < n; i ++)
    {
        f >> type;
        switch(type)
        {
            case 1:
                f >> x;
                L ++; NR ++;
                A[NR] = x;
                Heap[L] = NR;
                Pos[NR] = L;
                push(L);
            break;
            case 2:
                f >> x;
                A[x] = -1;
                push(Pos[x]);

                Pos[Heap[1]] = 0;
                Heap[1] = Heap[L--];
                Pos[Heap[1]] = 1;
                if(L > 1) pop(1);
            break;
            case 3:
                g << A[Heap[1]] << endl;
            break;
        }
    }

    return 0;
}