Cod sursa(job #1615927)

Utilizator Vali_DeaconuVali Deaconu Vali_Deaconu Data 26 februarie 2016 23:01:10
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
# include <fstream>
# include <algorithm>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

const int MAX_HEAP_SIZE = 200010;
int h[MAX_HEAP_SIZE];
int r[MAX_HEAP_SIZE];
int x, t, p;

bool comp(const int& a, const int& b) {
    return a > b;
}

int Search(int val) {
    int st, dr, mid;
    st = 1; dr = h[0];
    while (st <= dr) {
        mid = st + ((dr - st) / 2);

        if (h[mid] > val)
            dr = mid - 1;
        else if (h[mid] <= val)
            st = mid + 1;
    }
    return dr;
}

int main() {
    make_heap(h+1, h+h[0]+1, comp);

    fin >> t;
    while (t--) {
        fin >> p;
        if (p == 1) {
            fin >> x;
            h[++h[0]] = r[++r[0]] = x;
            push_heap(h+1, h+h[0]+1, comp);

            continue;
        }

        if (p == 2) {
            int i;
            fin >> x;
            i = Search(r[x]);

            pop_heap(h+i, h+h[0]+1, comp);
            h[0]--;

            continue;
        }

        if (p == 3) {
            fout << h[1] << "\n";

            continue;
        }
    }

    fin.close();
    fout.close();
    return 0;
}