Cod sursa(job #3135150)

Utilizator LazarDanielGabrielLazar Daniel-Gabriel LazarDanielGabriel Data 2 iunie 2023 00:42:14
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <iostream>
#include <fstream>
#include <set>
#include <queue>
#include <cmath>
using namespace std;

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

struct pereche
{
    int a, b;
    pereche(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }

    pereche& operator=(const pereche& per)
    {
        if (this != &per)
        {
            this->a = per.a;
            this->b = per.b;
        }
        return *this;
    }

    bool operator<(const pereche& per) const
    {
        return abs(this->a - this->b) < abs(per.a - per.b);
    }

    bool operator>(const pereche& per) const
    {
        return abs(this->a - this->b) > abs(per.a - per.b);
    }
};

priority_queue<pereche, vector<pereche>, greater<pereche>> PQ;
set<int> Z;

void update(int x)
{
    int minim = 1000000010, nr = x;
    auto it = Z.find(x);
    if (it != Z.end())
    {
        if (it != Z.begin())
        {
            auto pred = it;
            --pred;
            minim = abs(x - *pred);
            nr = *pred;
        }
        if (++it != Z.end())
        {
            if (abs(x - *it) < minim)
                nr = *it;
        }
    }
    if (nr != x)
        PQ.push(pereche(x, nr));
}

void inserare(int x)
{
    if (!Z.count(x))
    {
        Z.insert(x);
        update(x);
    }
}

int stergere(int x)
{
    if (Z.count(x))
    {
        auto it = Z.find(x);
        auto pred = it;
        auto succ = it;

        if (it != Z.begin())
        {
            auto pred = it;
            --pred;
        }
        if (++succ != Z.end())
        {
            PQ.push(pereche(*pred, *succ));
        }

        Z.erase(x);
        return 1;
    }
    return -1;
}

int cauta(int x)
{
    return Z.count(x);
}

int maxim()
{
    if (Z.size() < 2)
        return -1;
    auto minim = Z.begin();
    auto maxim = Z.rbegin();
    return abs(*minim - *maxim);
}

int minim()
{
    if (Z.size() < 2)
        return -1;

    pereche x = PQ.top();
    while (!Z.count(x.a) || !Z.count(x.b))
    {
        PQ.pop();
        if (Z.count(x.a))
            update(x.a);
        else if (Z.count(x.b))
            update(x.b);
        x = PQ.top();
    }
    return abs(x.a - x.b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string comanda;
    int x;
    while (fin >> comanda)
    {
        if (comanda == "I")
        {
            fin >> x;
            inserare(x);
        }
        else if (comanda == "S")
        {
            fin >> x;
            int val = stergere(x);
            if (val == -1)
                fout << -1 << "\n";
        }
        else if (comanda == "C")
        {
            fin >> x;
            fout << cauta(x) << "\n";
        }
        else if (comanda == "MAX")
        {
            fout << maxim() << "\n";
        }
        else if (comanda == "MIN")
        {
            fout << minim() << "\n";
        }
    }

    return 0;
}