Cod sursa(job #2734088)

Utilizator Iulia25Hosu Iulia Iulia25 Data 31 martie 2021 13:00:38
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.14 kb
#include <fstream>
#include <set>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

ifstream cin ("zeap.in");
ofstream cout ("zeap.out");

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

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

int main()  {
    string type;
    int x;
    while (cin >> type) {
        if (type == "I")    {
            cin >> x;
            if (s.find(x) == s.end())   {
                s.insert(x);
                auto it = s.find(x);
                if (it != s.begin())    {
                    auto it2 = it;
                    --it2;
                    pq.push({(*it), (*it2)});
                }
                auto it2 = it;
                ++it2;
                if (it2 == s.end())
                    continue;
                pq.push({(*it), (*it2)});
            }
        } else if (type == "MAX")  {
            if (s.size() < 2)   {
                cout << "-1\n";
                continue;
            }
            auto it_max = s.end();
            --it_max;
            auto it_min = s.begin();
            cout << (*it_max) - (*it_min) << '\n';
        } else if (type == "S")    {
            cin >> x;
            if (s.find(x) == s.end())
                cout << "-1\n";
            else    {
                auto it = s.find(x);
                auto it_max = it;
                ++it_max;
                if (it != s.begin() && it_max != s.end())  {
                    --it;
                    pq.push({(*it), (*it_max)});
                }
                s.erase(x);
            }
        } else if (type == "C")    {
            cin >> x;
            if (s.find(x) == s.end())
                cout << "0\n";
            else cout << "1\n";
        } else  {
            if (s.size() < 2)   {
                cout << "-1\n";
                continue;
            }
            while (s.find(pq.top().first) == s.end() || s.find(pq.top().second) == s.end())
                pq.pop();
            cout << abs(pq.top().first - pq.top().second) << '\n';
        }
    }
    return 0;
}