Mai intai trebuie sa te autentifici.

Cod sursa(job #1089984)

Utilizator Ionut228Ionut Calofir Ionut228 Data 22 ianuarie 2014 09:53:54
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int N, M;
int nr;
int H[200002], poH[200002], posAdd[200002];

int father(int nod)
{
    return nod / 2;
}

int left_son(int nod)
{
    return 2 * nod;
}

int righ_son(int nod)
{
    return 2 * nod + 1;
}

void percolate(int nod) // urca
{
    int key = H[nod];
    int pH = posH[posAdd[nod]];
    int pA = posAdd[nod];

    while ((nod > 1) && key < H[father(nod)])
    {
        H[nod] = H[father(nod)];
        posH[posAdd[nod]] = posH[posAdd[father(nod)]];
        posAdd[nod] = posAdd[father(nod)];
        //swap(H[nod], H[father(nod)]);
        //swap(posH[posAdd[nod]], posH[posAdd[father(nod)]);
        //swap(posAdd[nod], posAdd[father(nod)]);
        nod = father(nod);
    }

    H[nod] = key;
    posH[posAdd[nod]] = pH;
    posAdd[nod] = pA;
}

void sift(int nod) // coboara
{
    int son = 1;
    while (son)
    {
        son = 0;
        if (left_son(nod) <= N && H[nod] > H[left_son(nod)] && H[left_son(nod)] < H[right_son(nod)]) son = left_son(son);
        else if (right_son(nod) <= N && H[nod] > H[right_son(nod)] && H[right_son(nod)] < H[left_son(son)]) son = right_son(son);
        else son = 0;

        if (son)
        {
            swap(H[nod], H[son]);
        }
    }
}
int main()
{
    fin >> M;
    for (int i = 1, cod; i <= M; ++i)
    {
        fin >> cod;
        int numar;
        if (cod == 1)
        {
            fin >> numar;

            ++N;
            ++nr;
            H[N] = numar;
            posAdd[N] = nr;
            posH[nr] = N;

            percolate(N);
        }
        else if (cod == 2)
        {

        }
        else fout << H[1] << '\n';
    }

    fin.close();
    fout.close();
    return 0;
}