Cod sursa(job #2892421)

Utilizator Rares_StefanoiuRares Stefanoiu Rares_Stefanoiu Data 22 aprilie 2022 02:14:22
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.35 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;
                }
            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  
                while (fiu <= c) {
                    int min = fiu;
                    int st = 2 * fiu + 1;
                    int dr = 2 * fiu + 2;


                    if ( st<c and heap[st] < heap[min])
                        min = st;


                    if (dr<c and heap[dr] < heap[min])
                        min = dr;

                    if (min != fiu)
                        swap(heap[fiu], heap[min]);
                    else
                        break;
                }
            
        }
        if (x == 3)
            g<< heap[0]<<"\n";

    }
        
    return 0;
}