Cod sursa(job #3130107)

Utilizator AnaRosuAna Maria Rosu AnaRosu Data 16 mai 2023 21:06:22
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.98 kb
#include <fstream>
#include <vector>
#include <unordered_map>

using namespace std;

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

class MinHeap {
private:
    vector<int> elem;
    vector<int> poz;
    vector<pair<int, int>> heap;

public:
    MinHeap() {
        elem.push_back(0);
        heap.push_back(make_pair(0, 0));
        poz.push_back(0);
    }
    void repairHeapDown(int pos){
        if (heap[pos].first < heap[pos / 2].first && pos / 2 >= 1) {
            while (heap[pos].first < heap[pos / 2].first && pos / 2 >= 1) {
                swap(poz[heap[pos].second], poz[heap[pos / 2].second]);
                swap(heap[pos], heap[pos / 2]);
                pos = pos / 2;
            }
        } else {
            while ((pos * 2 < heap.size() && heap[pos * 2].first < heap[pos].first ) ||
                   (pos * 2 + 1 < heap.size() && heap[pos * 2 + 1].first < heap[pos].first)) {

                if (pos * 2 + 1 >= heap.size() || heap[pos * 2].first < heap[pos * 2 + 1].first) {
                    swap(poz[ heap[pos * 2].second], poz[heap[pos].second]);
                    swap(heap[pos], heap[pos * 2]);
                    pos = pos * 2;
                } else {
                    swap(poz[heap[pos * 2 + 1].second], poz[heap[pos].second]);
                    swap(heap[pos], heap[pos * 2 + 1]);
                    pos = pos * 2 + 1;
                }
                
            }
        }
    }
    void repairHeapUp(int pos){
        while(pos){
        int dad = pos/2;
        if (heap[dad] > heap[pos]){
            swap(poz[heap[pos].second], poz[heap[dad].second]);
            swap(heap[dad],heap[pos]);
            pos = dad;
        }
        else break;
        }
    }
     // insert element x
    void insert(int x) {
        heap.push_back(make_pair(x, elem.size()));
        poz.push_back(heap.size() - 1);
        elem.push_back(x);
        repairHeapUp(heap.size()-1);
    }

    void del(int i) {
        //find the index of the element that was the i-th inserted
        int pos = poz[i];
        //move the last element over it
        heap[pos] = heap[heap.size() - 1];
        poz[ heap[heap.size() - 1].second ] = poz[i];
        // delete the last element
        heap.pop_back();
        elem[i] = -1;
        poz[i] = -1;
        repairHeapDown(pos);
    }

    // print minimum element
    void mini(){
        g<<heap[1].first<<endl;
    }
};

int main() {
    MinHeap H;
    int n, a, b;
    f >> n;
    for (int i = 0; i < n; i++) {
        f >> a;
        switch (a) {
            case 1:{
            f >> b;
            H.insert(b);
            break;
            }
            case 2: {
            f >> b;
            H.del(b);
            break;
            }
            case 3: {
            H.mini();
            break;
            }
            default:{
            break;
            }
        }
    }
    f.close();
    g.close();
    return 0;
    }