Cod sursa(job #3126501)

Utilizator TirilaPatricTirila Patric-Gabriel TirilaPatric Data 6 mai 2023 18:09:07
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.87 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <deque>
#include <vector>
#include <unordered_map>
using namespace std;

class Heap{
    vector<int> values;
    vector<int> order;
public:
    Heap();
    void insert(int);
    int removeMin();
    void remove(int);
    void heapify(int);
    int getMin();
    void showHeap();
};
void Heap::showHeap(){
    cout<<"This is your heap: "<<endl;
    for(int i=0; i<this->values.size(); i++){
        cout<<this->values[i]<<" ";
    }
    cout<<endl;
}
Heap::Heap(){
    this->values = {};
    this->order = {-1};
}
void Heap::heapify(int i){
    if(this->values.size() == 2){
        if(this->values[0] > this->values[1]){
            int aux = this->values[1];
            this->values[1] = this->values[0];
            this->values[0] = aux;
        }
    } else {
        while (2 * i + 2 < this->values.size()) {
            int j;
            if (this->values[2 * i + 1] < this->values[2 * i + 2]) {
                j = 2 * i + 1;
            } else {
                j = 2 * i + 2;
            }
            if (this->values[i] > this->values[j]) {
                int aux = this->values[j];
                this->values[j] = this->values[i];
                this->values[i] = aux;
                i = j;
            } else {
                break;
            }
        }
    }
}
void Heap::insert(int value){
    this->order.push_back(value);
    this->values.push_back(value);
    int i = this->values.size()-1;
    while((i-1)/2 >= 0 && this->values[i] < this->values[(i-1)/2]){
        int aux = this->values[(i-1)/2];
        this->values[(i-1)/2] = this->values[i];
        this->values[i] = aux;
        i = (i-1)/2;
    }
}
int Heap::removeMin(){
    int R = this->values[0];
    this->values[0] = this->values[this->values.size()-1];
    this->values.pop_back();
    int i = 0;
    this->heapify(i);
    return R;
}
void Heap::remove(int pos){
    int el = this->order[pos];
    int i;
    for(int k=0; k<this->values.size(); k++){
        if(this->values[k] == el){
            i = k;
        }
    }
    int aux = this->values[this->values.size()-1];
    this->values[this->values.size()-1] = this->values[i];
    this->values[i] = aux;
    this->values.pop_back();
    this->heapify(i);
}
int Heap::getMin(){
    return this->values[0];
}
int main(){
    ifstream f("heapuri.in");
    ofstream g("heapuri.out");
    int n, a, b;
    f>>n;

    Heap h;
    while(n>0){
        f>>a;
        switch(a){
            case 1: {
                f>>b;
                h.insert(b);
                break;
            }
            case 2: {
                f>>b;
                h.remove(b);
                break;
            }
            case 3: {
                int min = h.getMin();
                g<<min<<endl;
                break;
            }
            default: {
                break;
            }
        }
        n--;
    }

    return 0;
}