Cod sursa(job #3130220)

Utilizator davidtoma11Toma David davidtoma11 Data 17 mai 2023 01:50:19
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

set<int> heap;
int arr[200005];
int n, c = 0;

void heapifyUp(int idx) {
    while (idx > 1 && arr[idx] < arr[idx / 2]) {
        swap(arr[idx], arr[idx / 2]);
        idx /= 2;
    }
}

void heapifyDown(int idx) {
    int child;
    while (2 * idx <= c) {
        child = 2 * idx;
        if (child + 1 <= c && arr[child + 1] < arr[child])
            child++;

        if (arr[child] < arr[idx]) {
            swap(arr[child], arr[idx]);
            idx = child;
        } else {
            break;
        }
    }
}

int main() {
    fin >> n;

    for (int i = 1; i <= n; i++) {
        int op, x;
        fin >> op;

        if (op == 1) {
            fin >> x;
            c++;
            arr[c] = x;
            heapifyUp(c);
        } else if (op == 2) {
            fin >> x;
            int idx = find(arr + 1, arr + c + 1, x) - arr;
            swap(arr[idx], arr[c]);
            c--;
            heapifyUp(idx);
            heapifyDown(idx);
        } else {
            fout << arr[1] << '\n';
        }
    }

    fin.close();
    fout.close();

    return 0;
}