Cod sursa(job #2290061)

Utilizator alexnigaNiga Alexandru alexniga Data 25 noiembrie 2018 18:24:54
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

struct heap
{
    int num, poz;
};

heap aux, x[200001];

void heapup(int pas)
{
    while ( pas != 1 && x[pas / 2].num > x[pas].num)
    {
        aux = x[pas];
        x[pas] = x[pas / 2];
        x[pas / 2] = aux;
        pas /= 2;
    }
}

void heapdown(int pas, int k)
{
    while(2 * pas <= k && x[pas].num > x[pas * 2].num)
    {
        aux = x[pas];
        x[pas] = x[pas * 2];
        x[pas * 2] = aux;
        pas *= 2;
    }
}

int main()
{
    int n, i, j, pas, verf, nr, k = 0, numar = 0;
    f >> n;

    for (i = 0; i < n; ++i)
    {
        f >> verf;
        if (verf != 3)
        {
            f >> nr;

            if( verf == 1)
            {
                ++k; ++numar;
                x[k].num = nr;
                x[k].poz = numar;
                pas = k;
                heapup(pas);
            }
            else
            {
                for (j = 1; j <= k; j++)
                    if (x[j].poz == nr)
                        {
                            x[j] = x[k];
                            k--;
                        heapdown(j, k);
                        j = k + 1;
                        }
           }
    }
    else
        g << x[1].num << "\n";

}



return 0;
}