Cod sursa(job #2895362)

Utilizator biancar28Radulescu Alexia-Bianca biancar28 Data 29 aprilie 2022 00:06:50
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 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 heapify(int i){
    int mini = i;
    int st = 2 * i + 1;
    int dr = 2 * i + 2;

    if (st < V.size() && V[st] < V[mini])
        mini = st;

    if (dr < V.size() && V[dr] < V[mini])
        mini = dr;

    if (mini != i) {
        swap(V[i], V[mini]);
        swap(Poz[i], Poz[mini]);
        heapify(mini);
    }
}



int main() {

    int cod,x,nr=0,i,j;
    f>>n;
    while(n!=0)
    {
        f>>cod;
        if(cod==1){
            f>>x;
            nr++;
            V.push_back(x);
            Poz.push_back(nr);
            for (i = V.size() / 2 - 1; i >= 0; i--)
            {
                heapify(i);
                for(j=0;j<V.size();j++)
                {
                    cout<<V[j]<<" ";
                }
                cout<<endl;
            }

        }
        else if(cod==2){
            f>>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();
            for (i = V.size() / 2 - 1; i >= 0; i--)
                heapify(i);

            for(j=0;j<V.size();j++)
            {
                cout<<V[j]<<" ";
            }
            cout<<endl;
        }
        else if(cod==3){
            g<<V[0]<<"\n";
        }

        n--;
    }

    return 0;
}