Cod sursa(job #3135134)

Utilizator LazarDanielGabrielLazar Daniel-Gabriel LazarDanielGabriel Data 1 iunie 2023 22:26:24
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

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

unordered_set<int> zeap;
vector<int> sortedZeap;

void inserare(int x)
{
    if (zeap.count(x) == 0)
    {
        zeap.insert(x);
        sortedZeap.push_back(x);
        sort(sortedZeap.begin(), sortedZeap.end());
    }
}

int stergere(int x)
{
    if (zeap.count(x) == 1)
    {
        zeap.erase(x);
        sortedZeap.erase(find(sortedZeap.begin(), sortedZeap.end(), x));
        return 1;
    }
    return -1;
}

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

int maxDif()
{
    if (sortedZeap.size() < 2)
    {
        return -1;
    }
    return sortedZeap.back() - sortedZeap.front();
}

int minDif()
{
    if (sortedZeap.size() < 2)
    {
        return -1;
    }
    int minDiff = INT_MAX;
    for (size_t i = 1; i < sortedZeap.size(); i++)
    {
        minDiff = min(minDiff, sortedZeap[i] - sortedZeap[i - 1]);
    }
    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;
}