Cod sursa(job #2894894)

Utilizator biancar28Radulescu Alexia-Bianca biancar28 Data 28 aprilie 2022 15:48:29
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.68 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n;
vector<int>V;
vector<int>Poz;

void insert(int x,int nr) {
    int i;
    V.push_back(x);
    Poz.push_back(nr);
    i = V.size() - 1;
    while (i) {
        if (V[i] < V[(i-1)/2]) {
            swap(V[i], V[(i-1)/2]);
            swap(Poz[i], Poz[(i-1)/2]);
            i = (i-1)/2;
        }
        else {
            break;
        }
    }
}

void arrange(int i) {
    if (i * 2 + 1 >= V.size())
        return;
    int st = V[i * 2 + 1];
    if ((i * 2 + 2 == V.size()) || st < V[i * 2 + 2]){
        if (st < V[i]) {
            swap(V[i], V[i * 2 + 1]);
            swap(Poz[i], Poz[i * 2 + 1]);
            arrange(i * 2 + 1);
            return;
        }
        else {
            return;
        }
    }
    else {
        if (V[i * 2 + 2] < V[i]) {
            swap(V[i], V[i * 2 + 2]);
            swap(Poz[i], Poz[i * 2 + 2]);
            arrange(i * 2 + 2);
            return;
        }
        else {
            return;
        }
    }
}

void erase(int x) {
    int i;
    for (i = 0; i < Poz.size(); i++) {
        if (Poz[i] == x) {
            break;
        }
    }
    V[i] = V[V.size() - 1];
    Poz[i] = Poz[Poz.size() - 1];
    V.pop_back();
    Poz.pop_back();
    arrange(i);


}


int main() {

    int cod,x,nr=-1;
    f>>n;
    while(n!=0)
    {
        f>>cod;
        if(cod==1){
            f>>x;
            nr++;
            insert(x,nr);
        }
        else if(cod==2){
            f>>x;
            x = x-1;
            erase(x);
        }
        else if(cod==3){
            g<<V[0]<<"\n";
        }

        n--;
    }

    return 0;
}