Cod sursa(job #2906508)

Utilizator utilizator20052022utilizatorr utilizator20052022 Data 26 mai 2022 14:20:43
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.19 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

void swapElements(vector<int> &h, int a, int b){
    int aux = h[a];
    h[a] = h[b];
    h[b] = aux;
}

int getParentIndex(int index){
    return (index - 1) / 2;
}

bool hasParent(vector<int> h, int index){
    if(getParentIndex(index) >= 0 && getParentIndex(index) < h.size()) return true;
    return false;
}

int parent(vector<int> h, int index)
{
    return h[getParentIndex(index)];
}

void heapifyUp(vector<int> &h)
{
    int index = h.size() - 1;
    while(hasParent(h, index) && parent(h,index) > h[index])
    {
        swapElements(h, getParentIndex(index), index);
        index = getParentIndex(index);
    }
}

void insertInHeap(vector<int> &h, int value)
{
    h.push_back(value);
    heapifyUp(h);
}

int leftChildIndex(int index){
    return index * 2 + 1;
}

int rightChildIndex(int index){
    return index * 2 + 2;
}

bool hasRightChild(vector<int> h, int index)
{
    if (rightChildIndex(index) > 0 && rightChildIndex(index) < h.size() ) return true;
    return false;
}

bool hasLeftChild(vector<int> h, int index)
{
    if (leftChildIndex(index) > 0 && leftChildIndex(index) < h.size() ) return true;
    return false;
}
// void printHeap(vector<int> h)
// {
//     for(auto x: h){
//         cout<<x<<' ';
//     }
//     cout<<'\n';
// }

void heapifyDown(vector<int> &h, int index){
    //voi incepe de la index
    int minChildIndex = leftChildIndex(index);
    while(hasLeftChild(h,index)){
        if(hasRightChild(h, index) && h[rightChildIndex(index)] < h[leftChildIndex(index)]){
            minChildIndex = rightChildIndex(index);
        }
        if(h[index] < h[minChildIndex]) return;
        else
        {
            swapElements(h, minChildIndex, index);
        }
        index = minChildIndex;
    }
}

void removeFromHeap(vector<int> &h, int index)
{
    h[index] = h[h.size() - 1];
    h.pop_back();
    heapifyDown(h, index);
}

void afiseazaMin(vector<int> h)
{
    //este garantat ca nu se cere minimul pt mult vida
    fout<<h[0]<<'\n';
}

int getIndex(vector<int> &h, int value){
    for(int i = 0; i < h.size(); ++i){
        if(h[i] == value) return i;
    }
}

int main(){
    int N, cod, value, indexCron = 1;
    fin>>N;

    vector<int> h;
    vector<int> cronologic;
    cronologic.resize(200001); //retin pentru fiecare element al catelea a intrat a i cronologic[0] = primul element si tot asa
    //este garantat ca nu vor exista 2 operatii de tipul 2 care sa stearga acelasi element din multime si ca
    //Pentru o operatie de tipul 2 se va sterge un element care e deja intrat in multime

    for(int i = 0; i < N; ++i)
    {
        fin>>cod;
        if(cod == 1){
            fin>>value;
            insertInHeap(h,value);
            cronologic[indexCron] = value;
            ++indexCron;
        }
         if(cod == 2){
            fin>>value;
            removeFromHeap(h, getIndex(h,cronologic[value]));
        }
        if(cod == 3){
           afiseazaMin(h); 
        }

        //printHeap(h);
    }

    return 0;
}