Cod sursa(job #3243200)

Utilizator Dragu_AndiDragu Andrei Dragu_Andi Data 16 septembrie 2024 15:50:35
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <fstream>

using namespace std;

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

bool scoase[200001];
pair<int,int> heap[200001];
int n=0, m=0;

int top()
{
    while(scoase[heap[1].second])
    {
        swap(heap[1],heap[n]);
        heap[n]={1000000001, -1};
        n--;
        int i=1;
        while(heap[i].first>heap[i*2].first || heap[i].first>heap[i*2+1].first)
        {
            if(heap[i*2].first < heap[i*2+1].first)
            {
                swap(heap[i],heap[i*2]);
                i*=2;
            }
            else
            {
                swap(heap[i],heap[i*2+1]);
                i=i*2+1;
            }
        }
    }
    return heap[1].first;
}

void push(int x)
{
    heap[++n]={x,++m};
    int i=n;
    while(heap[i].first<heap[i/2].first)
    {
        swap(heap[i], heap[i/2]);
        i/=2;
    }
}

int main()
{
    int q;
    fin >> q;
    heap[0]={-1,-1};
    for(int i=1; i<200000; i++)
        heap[i]={1000000001, -1};
    while(q--)
    {
        int c;
        fin >> c;
        if(c==3)
        {
            fout << top() << '\n';
        }
        else if(c==2)
        {
            int n;
            fin >> n;
            scoase[n]=1;
        }
        else
        {
            int n;
            fin >> n;
            push(n);
        }
    }
    return 0;
}