Cod sursa(job #2895738)

Utilizator Teodor_AxinteAxinte Teodor-Ionut Teodor_Axinte Data 29 aprilie 2022 13:57:41
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.4 kb

#include <bits/stdc++.h>

using namespace std;

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

vector<long long> poz;
multiset <long long > heap;

int n,x,op;


int main(){
    fin>>n;
    for(;n!=0;n--){
        fin>>op;
        if(op==1){
            fin>>x;
            heap.insert(x);
            poz.push_back(x);
        }
        else if(op==2){
            fin>>x;
            heap.erase(poz[x-1]);
        }
        else if(op==3){
            fout<<(*heap.begin())<<'\n';
        }
    }

    return 0;
}

/*
#include <bits/stdc++.h>

using namespace std;

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

static const int oo = 999999;

class minHeap {
private:
    int Size = 0;
    vector<int> vec = {oo};

    int left(int i) { return i * 2; } // i<<1
    int right(int i) { return i * 2 + 1; }// i<<1 + 1
    int previous(int i) { return i / 2; }

public:
    bool isEmpty() const {
        return Size == 0;
    }

    int getMin() const {
        return vec[1];
    }

    void shiftUp(int value) {
        //if (value >= Size)
          //  return;
        if (value == 1)
           return;
        if (vec[value] < vec[previous(value)]) {
            swap(vec[value], vec[previous(value)]);
            shiftUp(previous(value));
        }
    }

    void shiftDown(int value){

        if(value>Size)
            return;
        int swapId = value;
        if(left(value) <=Size && vec[value] > vec[left(value)])
            swapId = left(value);
        if(right(value) <= Size && vec[swapId] > vec[right(value)])
            swapId=right(value);
        if(swapId!=value){
            swap(vec[value],vec[swapId]);
            shiftDown(swapId);
        }

    }



    void ExtractPos(int value){
        int pos = vec[value];
        swap(vec[pos],vec[Size--]);
        shiftDown(pos);
    }
    void insertItem(int value) {
      //  if (Size + 1 >= vec.size())
           // vec.push_back(oo);
        vec.push_back(value);
        Size++;
        shiftUp(Size);
    }

    void display() {
        for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
            cout << (*it) << " ";
    }
};

int main() {

    minHeap *HEAP = new minHeap;

    HEAP->insertItem(43);
    HEAP->insertItem(89);
    HEAP->insertItem(32);
    HEAP->insertItem(15);
    HEAP->insertItem(51);
    HEAP->insertItem(99);
    HEAP->insertItem(33);


    HEAP->ExtractPos(3);

    HEAP->display();


    return 0;
}*/