Cod sursa(job #2077036)

Utilizator Eduard24Eduard Scaueru Eduard24 Data 27 noiembrie 2017 17:11:23
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>
#include <algorithm>
#include <iostream>

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

int n,x,v[200003],i,a[200003],m,y,nr,j,h[200003],l;

void Up(int k, int m)
{
    int t,f;
    f=k;
    t=f/2;
    while(t && v[h[t]]>v[h[f]])
    {
        swap(h[t],h[f]);
        f=t;
        t=f/2;
    }
}

void Down(int k, int m)
{
    int t,f;
    t=k;
    f=k*2;
    while(f<=m)
    {
        if(f<m)
        {
            if(v[h[f]]>v[h[f+1]]) f++;
        }
        if(v[h[t]]>v[h[f]])
        {
            swap(h[t],h[f]);
            t=f;
            f=f*2;
        }
        else
        {
            f=m+1;
        }
    }
}

void Inserare(int y)
{
    m++;
    l++;
    v[m]=y;
    a[m]=0;
    h[l]=m;
    Up(l,l);
}

int Minim()
{
    while(a[h[1]]==1)
    {
        h[1]=h[l];
        l--;
        Down(1,l);
    }
    return v[h[1]];
}

int main()
{
    fin>>n;
    m=0;
    l=0;
    for(i=1;i<=n;i++)
    {
        fin>>x;
        if(x==1)
        {
            fin>>y;
            Inserare(y);
        }
        if(x==2)
        {
            fin>>y;
            a[y]=1;
        }
        if(x==3)
        {
            fout<<Minim()<<"\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}