Cod sursa(job #1538293)

Utilizator ilinoiuflaviusIlinoiu Flavius ilinoiuflavius Data 28 noiembrie 2015 19:18:12
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <fstream>
#define nmax 200001
using namespace std;

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

int n,v[nmax],cron[nmax],k;
void exch(int a,int b)
{
    int aux;
    aux=v[a];
    v[a]=v[b];
    v[b]=aux;
}
void down(int poz)
{
    while(poz*2 <= k)
    {
        if(poz*2+1 > k)
        {
            if(v[poz]>v[poz*2])exch(poz,poz*2),poz=poz*2;
        }
        else
        {
            if(v[poz*2] < v[poz*2+1])
            {
                if(v[poz]>v[poz*2])exch(poz,poz*2),poz=poz*2;
            }
            else if(v[poz]>v[poz*2+1])exch(poz,poz*2+1),poz=poz*2+1;
        }
    }
}
void add()
{
    int x; f>>x;
    k++;
    v[k]=x;
    cron[k]=x;

    int z=k;
    while(z/2)
    {
        if(v[z] < v[z/2])
            exch(z,z/2);
        z=z/2;
    }
}

void substr()
{
    int x;
    f>>x;
    x=cron[x];
    for(int i=1;i<=k;i++)
    {
        if(x==v[i])
        {
            x=i;
            break;
        }
    }
    exch(x,k);
    k--;
    down(x);
}

void min()
{
    g<<v[1]<<endl;
}
void read()
{
    int a,b;
    f>>n;
    for(int i=1;i<=n;i++)
    {
        f>>a;
        if(a==1)add();
        else if(a==2)substr();
        else min();
        //cout<<"sir:  ";
        //for(int i=1;i<=k;i++)cout<<v[i]<<" ";cout<<endl;
    }

}

int main()
{
    read();
    return 0;
}