Cod sursa(job #2622474)

Utilizator ihorvaldsTudor Croitoru ihorvalds Data 1 iunie 2020 13:02:08
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>

int binary(std::vector<int>& v, int sought) {

    int i, step = 1;
    for (; step < v.size(); step <<= 1);
    for (i = 0; step; step >>=1) {
        if (i + step < v.size() && v[i+step] <= sought) {
            i+=step;
        }
    }
    if (v[i] == sought) {
        return i;
    }
    return -1;
}

void push(std::vector<int>& v, int val) {
    int i = 0;
    for (; i < v.size() && v[i] < val; i++);
    v.insert(v.begin() + i, val);
}

void remove(std::vector<int>& v, int val) {
    int found = binary(v, val);

    if (found != -1)
        v.erase(v.begin() + found);
}

int main() {
    std::ifstream f("heapuri.in");
    std::ofstream g("heapuri.out");

    std::vector<int> v, ord;
    int command, n, tmp;
    f >> n;
    for (int i = 0; i < n; i++) {
        f >> command;

        if (command == 1) {
            f >> tmp;
            push(v, tmp);
            ord.push_back(tmp);
        }

        if (command == 2) {
            f >> tmp;
            remove(v, ord[tmp - 1]);
        }

        if (command == 3) {
            g << v[0] << "\n";
        }
    }
}