Cod sursa(job #2412748)

Utilizator CybotStancila Ionut-Marian Cybot Data 22 aprilie 2019 15:22:01
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n, c, x, poz[200005], h[200005], k=0, pozitie=0;

void hcomb(int i, int l)
{
    int v=h[i], tata=i, fiu=2*tata;
    while (fiu<=l)
    {
        if (fiu<l)
        if (h[fiu]>h[fiu+1]) fiu++;
        if (v>h[fiu])
        {
            h[tata]=h[fiu];
            tata=fiu;
            fiu=2*fiu;
        }
        else fiu=l+1;
    }
    h[tata]=v;
}

void hins (int nr)
{
    int fiu=++k, tata=k/2;
    while (tata && h[tata]>nr)
    {
        h[fiu]=h[tata];
        fiu=tata;
        tata=tata/2;
    }
    h[fiu]=nr;
}

int cautare(int nr)
{
    int i=1;
    while (h[i]!=nr)
    {
        i++;
    }
    return i;
}

void heliminare(int element)
{
    h[element]=h[k--];
    hcomb(element, k);
}

int main()
{
    f>>n;
    for (int i=1;i<=n;i++)
    {
        f>>c;
        if (c==1)
        {
            f>>x;
            hins(x);
            poz[++pozitie]=x;
        }
        else if (c==2)
        {
            f>>x;
            int pozitie=cautare(poz[x]);
            heliminare(pozitie);
        }
        else g<<h[1]<<'\n';
    }
    return 0;
}