Cod sursa(job #2894207)

Utilizator biancar28Radulescu Alexia-Bianca biancar28 Data 27 aprilie 2022 15:28:05
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 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>0){
        if(V[i]<V[i/2]){
            swap(V[i],V[i/2]);
            swap(Poz[i], Poz[i/2]);
            i = i/2;
        }
        else{
            break;
        }
    }
}

void erase(int x){
    int i;
    for(i=0;i<Poz.size();i++){
        if(Poz[i]==x){
            break;
        }
    }
    while(2*i+2<V.size()){
        if(V[2*i+1]<V[2*i+2]){
            swap(V[i],V[2*i+1]);
            swap(Poz[i],Poz[2*i+1]);
            i = 2*i+1;
        }
        else{
            swap(V[i],V[2*i+2]);
            swap(Poz[i],Poz[2*i+2]);
            i = 2*i+2;
        }
    }
    i +=1;
    while(i<V.size()){
        swap(V[i-1],V[i]);
        swap(Poz[i-1],Poz[i]);
        i++;
    }
    V.pop_back();
    Poz.pop_back();
}

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;
}