Cod sursa(job #2751863)

Utilizator ChelaruPaulChelaru Paul ChelaruPaul Data 15 mai 2021 23:19:24
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <queue>

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

auto cmp = [](pair<int, int> a, pair<int, int> b) {
    return abs(a.first - a.second) > abs(b.first - b.second);
};

set<int> s;
priority_queue<pair<int, int>, vector<pair<int, int> >, decltype(cmp)> pq(cmp);

void inserare() {
    int x;
    fin >> x;
    if (s.find(x) == s.end()) {
        s.insert(x);
        auto pz = s.find(x);
        if (pz != s.begin())
        {
            --pz;
            pq.push({*pz, x});
            ++pz;
        }
        if (pz != s.end()) {
            ++pz;
            if (pz != s.end()) {
                pq.push({x, *pz});
            }
        }
    }
}

void sterge() {
    int x;
    fin >> x;
    if (s.find(x) != s.end()) {
        auto it = s.find(x);
        auto it2 = it;
        ++it2;
        if (it != s.begin() && it2 != s.end()) {
            --it;
            pq.push({*it, *it2});
        }
        s.erase(x);
    } else fout << -1 << '\n';
}

void cauta() {
    int x;
    fin >> x;
    fout << (s.find(x) != s.end()) << '\n';
}

int maxx() {
    if (s.size() < 2)
        fout << -1 << '\n';
    else {
        auto it = s.end();
        --it;
        fout << *it - *s.begin()
             << '\n';
    }
}

int minn() {
    if (s.size() < 2)
        fout << -1 << '\n';
    else {
        while (s.find(pq.top().first) == s.end() || s.find(pq.top().second) == s.end())
            pq.pop();
        fout << abs(pq.top().first - pq.top().second)
             << '\n';
    }
}

int main() {
    string tip;

    while (fin >> tip) {
        if (tip == "I") {
            inserare();
        } else if (tip == "S") {
            sterge();
        } else if (tip == "C") {
            cauta();
        } else if (tip == "MAX") {
            maxx();
        } else {
            minn();
        }
    }

}