Cod sursa(job #2155663)

Utilizator cezinatorCezar D cezinator Data 7 martie 2018 23:29:24
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <fstream>

using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,cod,x,cron[200005],v[200005],HS,HC,poz[200005];
int son(int x)
{
    if(x*2+2>HS-1) return x*2+1;
    else if(v[x*2+1]>v[x*2+2]) return x*2+2;
    else return x*2+1;
}
int parent(int x)
{
    if(x%2==0) return x/2-1;
    else return x/2;
}
void bubble_up(int poz)
{
    int p=parent(poz);
    while(v[poz]<v[p]&&poz>1)
    {
        swap(v[poz],v[p]);
        poz=parent(poz);
        p=parent(poz);
    }
}
void bubble_down(int poz)
{
    int s=son(poz);
    while(v[poz]>v[s]&&poz*2+1<=HS-1)
    {
        swap(v[poz],v[s]);
        poz=son(poz);
        s=son(poz);
    }
}
void afis()
{
    for(int i=0;i<HS;i++)
        fout<<v[i]<<' ';
    fout<<'\n';
}
int gaseste(int x)
{
    for(int i=0;i<HS;i++)
        if(v[i]==x) return i;
}
void stergere(int x)
{
    int poz=gaseste(x);
    swap(v[poz],v[HS-1]);
    v[HS-1]=0;HS--;
    bubble_up(poz);
    bubble_down(poz);
}
void peek()
{
    fout<<v[0]<<'\n';
}
void inserare(int x)
{
    HS++;
    v[HS-1]=x;
    cron[HS]=x;
    if(HS-1!=0) bubble_up(HS-1);
}
int main()
{
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        fin>>cod;
        if(cod==1)
        {
            fin>>x;
            inserare(x);
        }
        else if(cod==2)
        {
            fin>>x;
            stergere(cron[x]);
        }
        else peek();
    }
    return 0;
}