Cod sursa(job #3135147)

Utilizator LazarDanielGabrielLazar Daniel-Gabriel LazarDanielGabriel Data 1 iunie 2023 23:54:13
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.13 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;
            ++it;
            minim = abs(x - *pred);
            nr = *pred;
        }
        if (it != --Z.end())
        {
            auto succ = ++it;
            --it;
            if (abs(x - *succ) < minim)
                nr = *succ;
        }
    }
    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;
            ++it;
        }
        if (it != --Z.end())
        {
            auto succ = ++it;
            --it;
        }
        if (pred != it && succ != it)
            PQ.push(pereche(*pred, *succ));

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

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

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()
{
    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;
}