Cod sursa(job #2892757)

Utilizator Cosmina_GheorgheGheorghe Cosmina Cosmina_Gheorghe Data 23 aprilie 2022 15:21:51
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.82 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");
struct elem
{
    int val, poz;
};
vector<elem> heapp;
void urca(int poz) {
    while (poz) {
    int tata = (poz - 1) / 2;
    if (heapp[tata].val > heapp[poz].val) {
    swap(heapp[tata], heapp[poz]);
    poz = tata;
    } else {
    break;
    }
    }
}

void push(elem yy) {
heapp.push_back(yy);
urca(heapp.size()-1);
}

void coboara(int poz) {
if (poz * 2 + 1 >= heapp.size())
    return;
int fiu_st = heapp[poz * 2 + 1].val;
if ((poz * 2 + 2 == heapp.size()) || fiu_st < heapp[poz * 2 + 2].val) {
    if (fiu_st < heapp[poz].val) {
        swap(heapp[poz], heapp[poz * 2 + 1]);
        coboara(poz * 2 + 1);
        return;
}
    else { return;
    }
}
else {
if (heapp[poz * 2 + 2].val < heapp[poz].val) {
    swap(heapp[poz], heapp[poz * 2 + 2]);
    coboara(poz * 2 + 2);
    return;
}
else {
return;
}
}}
int main()
{
    int n,x,y;
   f >> n;
   int cnt=0;
    for(int k = 0; k < n; k++)
    {
        f>>x;
        if(x==1)
        {   cnt++;
            f>>y;
            elem yy;
            yy.poz=cnt;
            yy.val=y;
            push(yy);
        }
        if(x==2)
        {
            f>>y;
            for(int i=0;i< heapp.size();i++)
            {
                if(heapp[i].poz==y)
                {
                    swap(heapp[i], heapp[heapp.size() - 1]);
                    heapp.pop_back();
                    urca(i);
                    coboara(i);
                    break;
                }
            }
        }
        if(x==3)
        {
            g<<heapp[0].val<<'\n';
        }
    }
    //for(int i = 0; i < heapp.size(); i++)
      //  cout << heapp[i].val << " ";
    return 0;
}