Cod sursa(job #2892420)

Utilizator Rares_StefanoiuRares Stefanoiu Rares_Stefanoiu Data 22 aprilie 2022 02:02:28
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.88 kb
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
void coboara(int v[], int n, int i)
{
    int min = i;
    int st = 2 * i + 1;
    int dr = 2 * i + 2;


    if (st < n and v[st] < v[min])
        min = st;


    if (dr < n and v[dr] < v[min])
        min = dr;

    if (min != i) {
        swap(v[i], v[min]);


        coboara(v, n, min);
    }
}
int n, i, x, c = 0, fiu, j, y, tata, p = 0;
int poz[200001], heap[200001];
int main() {
    ifstream f("heapuri.in");
    ofstream g("heapuri.out");
    f >> n;
    for (i = 1; i <= n; i++) {
        f >> x;
        if (x == 1) {
            f >> y;
            poz[p] = y;
            heap[c] = y;
            fiu = c;
            while (fiu) {
                 tata = (fiu - 1) / 2;
                if (heap[tata] > heap[fiu])
                {
                    swap(heap[tata], heap[fiu]);
                    fiu = tata;
                }
                else
                    break;
            }
            c++;
            p++;

        }
        if (x == 2) {
            f >> y;
            for (j = 0; j <= c; j++)
                if (heap[j] == poz[y-1]) {
                    fiu = j;
                    break;
                }
            //cout << fiu<<" "<<c;
            swap(heap[fiu] ,heap[--c]);
            if(fiu and heap[(fiu-1)/2]>heap[fiu])
                while (fiu) {
                    tata = (fiu - 1) / 2;
                    if (heap[tata] > heap[fiu])
                    {
                        swap(heap[tata], heap[fiu]);
                        fiu = tata;
                    }
                    else
                        break;
                }
            else  
               coboara(heap, c , fiu);
            
        }
        if (x == 3)
            g<< heap[0]<<"\n";

    }
        
    return 0;
}