Cod sursa(job #3135143)

Utilizator LazarDanielGabrielLazar Daniel-Gabriel LazarDanielGabriel Data 1 iunie 2023 22:56:12
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <iostream>
#include <fstream>
#include <set>
#include <climits>

using namespace std;

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

set<int> zeap;
int maxDiff = -1;
int minDiff = -1;

void updateDiffs()
{
    if (zeap.size() < 2)
    {
        maxDiff = -1;
        minDiff = -1;
    }
    else
    {
        maxDiff = *zeap.rbegin() - *zeap.begin();
        minDiff = INT_MAX;
        auto it = zeap.begin();
        auto nextIt = next(it);
        while (nextIt != zeap.end())
        {
            minDiff = min(minDiff, *nextIt - *it);
            ++it;
            ++nextIt;
        }
    }
}

void inserare(int x)
{
    if (zeap.count(x) == 0)
    {
        zeap.insert(x);
        updateDiffs();
    }
}

int stergere(int x)
{
    if (zeap.erase(x) > 0)
    {
        updateDiffs();
        return 1;
    }

    return -1;
}

int cauta(int x)
{
    return zeap.count(x) > 0 ? 1 : 0;
}

int maxDif()
{
    return maxDiff;
}

int minDif()
{
    return minDiff;
}

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 << maxDif() << "\n";
        }
        else if (comanda == "MIN")
        {
            fout << (minDiff == INT_MAX ? -1 : minDif()) << "\n";
        }
    }

    fin.close();
    fout.close();
    return 0;
}