Cod sursa(job #2743503)

Utilizator Ssebi1Dancau Sebastian Ssebi1 Data 23 aprilie 2021 09:17:33
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;

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

int heap[200001],order[200001];
int ctHeap = 0,ctOrder = 0;

void push(int k)
{
    ctHeap++;
    heap[ctHeap] = k;
    int i=ctHeap;

    // Fix the min heap property if it is violated
    while (i != 0 && heap[(i-1)/2] > heap[i])
    {
        swap(heap[i], heap[(i-1)/2]);
        i = (i-1)/2;
    }
}

void heapify(int i)
{
    int l = 2*i+1;
    int r = 2*i;
    int smallest = i;
    if (l < ctHeap && heap[l] < heap[i])
        smallest = l;
    if (r < ctHeap && heap[r] < heap[smallest])
        smallest = r;
    if (smallest != i)
    {
        swap(heap[i], heap[smallest]);
        heapify(smallest);
    }
}

void pop(int value)
{
    int poz=0;
    for(int i=1;i<=ctHeap;i++)
        if(heap[i]==value)
        {
            poz = i;
            break;
        }
    heap[poz] = INT_MAX;
    heapify(1);
    ctHeap--;
    heap[ctHeap+1] = 0;
}

int main() {
    int n;

    f>>n;

    for(int i=0;i<n;i++)
    {
        int op,value;
        f>>op;
        if(op==1)
        {
            f>>value;
            push(value);
            ctOrder++;
            order[ctOrder] = value;
        }
        else if(op==2)
        {
            f>>value;
            pop(order[value]);
        }
        else if(op==3)
        {
            g<<heap[1]<<'\n';
        }
    }


    return 0;
}