Cod sursa(job #3135136)

Utilizator LazarDanielGabrielLazar Daniel-Gabriel LazarDanielGabriel Data 1 iunie 2023 22:29:06
Problema Zeap Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <climits>

using namespace std;

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

unordered_set<int> zeap;

void inserare(int x)
{
    zeap.insert(x);
}

int stergere(int x)
{
    if (zeap.erase(x))
        return 1;
    return -1;
}

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

int maxDif()
{
    if (zeap.size() < 2)
        return -1;

    int maxDiff = INT_MIN;
    for (int num : zeap)
    {
        for (int otherNum : zeap)
        {
            if (num != otherNum)
            {
                maxDiff = max(maxDiff, abs(num - otherNum));
            }
        }
    }

    return maxDiff;
}

int minDif()
{
    if (zeap.size() < 2)
        return -1;

    int minDiff = INT_MAX;
    for (int num : zeap)
    {
        for (int otherNum : zeap)
        {
            if (num != otherNum)
            {
                minDiff = min(minDiff, abs(num - otherNum));
            }
        }
    }

    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 << minDif() << "\n";
        }
    }

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