Cod sursa(job #2894229)

Utilizator biancar28Radulescu Alexia-Bianca biancar28 Data 27 aprilie 2022 16:07:28
Problema Heapuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 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-1)/2]) {
            swap(V[i], V[(i-1)/2]);
            swap(Poz[i], Poz[(i-1)/2]);
            i = (i-1)/2;
        } else {
            break;
        }
    }
    cout<<"insert"<<endl;
    for(int j = 0; j < V.size(); j++){
        cout<<V[j]<<" ";
    }
    cout<<endl;
    for(int j = 0; j < Poz.size(); j++){
        cout<<Poz[j]+1<<" ";
    }
    cout<<endl<<"insertgata"<<endl;
}

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();
    cout<<"erase"<<endl;
    for(int j = 0; j < V.size(); j++){
        cout<<V[j]<<" ";
    }
    cout<<endl;
    for(int j=0; j < Poz.size(); j++){
        cout<<Poz[j]+1<<" ";
    }
    cout<<endl<<"erasegata"<<endl;
}

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";
            cout<<"min="<<V[0]<<endl;
        }

        n--;
    }

    return 0;
}