Cod sursa(job #3264952)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 25 decembrie 2024 23:15:23
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

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

struct cv {
    int x;
    int time;
};

int q;
vector<cv> heap;

bool cmp(const cv& a, const cv& b) {
    return a.x > b.x;
}

void push(const cv& elem) {
    heap.push_back(elem);
    push_heap(heap.begin(),heap.end(),cmp);
}

void pop(const int time) {
    int i = 0;
    while (heap[i].time != time) {
        i++;
    }
    swap(heap[i],heap.back());
    heap.pop_back();
    make_heap(heap.begin(),heap.end(),cmp);
}

int main() {
    fin >> q;
    int time = 1;
    while (q--) {
        int t;
        fin >> t;
        switch (t) {
            case 1: {
                int x;
                fin >> x;
                const cv temp = {x,time};
                push(temp);
                time++;
                break;
            }
            case 2: {
                int x;
                fin >> x;
                pop(x);
                break;
            }
            case 3: {
                fout << heap.front().x << '\n';
                break;
            }
        }
    }
    return 0;
}