Cod sursa(job #2811474)

Utilizator cezar.balutaCezar Baluta cezar.baluta Data 2 decembrie 2021 12:48:15
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

vector<int>heap;
vector<int>ordine;


void up(int poz){
    while(heap[poz]<heap[poz/2] && poz!=1){
        swap(heap[poz],heap[poz/2]);
        poz/=2;
    }
}

void down(int poz){
    int l = heap.size();
    while(poz*2 < l){
        int fiu = 2*poz;
        fiu+=(heap[fiu]>heap[fiu+1] && l>fiu+1);
        if(heap[fiu]>heap[poz])
            return;
        swap(heap[poz],heap[fiu]);
        poz=fiu;
    }
}

void ins(int x){
    heap.push_back(x);
    int poz=heap.size()-1;
    if(heap[poz]>heap[poz/2])
        up(poz);
}

void del(int poz){
    swap(heap[heap.size()-1],heap[poz]);
    heap.pop_back();
    up(poz);
    down(poz);
}

int main()
{
    ifstream in("heapuri.in");
    ofstream out("heapuri.out");
    heap.push_back(0);
    int n,cerinta,x;

    in>>n;
    for(int i=0;i<n;i++){
        in>>cerinta;
        if(cerinta == 1){
            in>>x;
            ins(x);
            ordine.push_back(x);

        }
        else if(cerinta == 2){
            in>>x;
            int pozitie;
            for(int i=0;i<ordine.size();i++){
                if(ordine[x-1] == heap[i]){
                    pozitie = i;
                    break;
                }
            }
            del(pozitie);

        }
        else{
            out<<heap[1]<<'\n';
        }
    }


    return 0;
}