Cod sursa(job #3163623)

Utilizator AlexandruDrg23Draghici Alexandru AlexandruDrg23 Data 31 octombrie 2023 18:21:25
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

const int cons=200003;
int n[cons],ord[cons];

ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
queue<int> q;

void sorta(int poz)
{
    while(n[poz]<n[poz>>1] && poz>>1!=0)
    {
        swap(n[poz],n[poz>>1]);
        poz=poz>>1;
    }
}

void ins(int arg)
{
    int k=1;
    while(n[k]!=-1)
        k++;
    n[k]=arg;
    sorta(k);
}

void ster(int poz)
{
    while(true)
    {
        if(n[poz<<1]==-1)
        {
            n[poz]=-1;
            break;
        }
        if(n[(poz<<1)+1]==-1)
        {
            swap(n[poz<<1],n[poz]);
            n[poz<<1]=-1;
            break;
        }
        if(n[poz<<1]<n[(poz<<1)+1])
        {
            swap(n[poz<<1],n[poz]);
            poz=poz<<1;
        }
        else
        {
            swap(n[(poz<<1)+1],n[poz]);
            poz=(poz<<1)+1;
        }
    }
}

void gas(int arg)
{
    for(int k=1;true;k++)
    {
        if(n[k]==ord[arg])
        {
            ster(k);
            return;
        }
    }
}

int main()
{
    int n;
    int op,arg;
    fin>>n;
    for(int k=1;k<=cons;k++)
        ::n[k]=-1;
    for(int k=1;k<=n;k++)
    {
        fin>>op;
        if(op==3)
            fout<<(::n[1])<<'\n';
        else
        {
            fin>>arg;
            if(op==1)
            {
                ins(arg);
                ord[++ord[0]]=arg;
            }
            else
                gas(arg);
        }
    }
    return 0;
}