Cod sursa(job #2751525)

Utilizator MihaiLazar26Lazar Mihai MihaiLazar26 Data 15 mai 2021 10:25:40
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <iostream>
#include <fstream>
#include <set>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");

set<int> Z;
void inserare(int x)
{
    Z.insert(x);
}

int sterge(int x)
{
    if(Z.count(x))
    {
        Z.erase(x);
        return 1;
    }

    else return -1;
}

int main()
{
    string comanda;
    int x;
    while(fin>>comanda)
    {
        if(comanda == "I")
        {
            fin>>x;
            inserare(x);
        }
        else if(comanda == "S")
        {
            fin>>x;
            int val = sterge(x);
            if(val == -1) fout<<-1<<"\n";
        }
        else if(comanda == "C")
        {
            fin>>x;
            fout<<Z.count(x)<<"\n";
        }
        else if(comanda == "MAX")
        {
            if(Z.size() >= 2)
            {
                auto maxim = Z.rbegin();
                auto minim = Z.begin();
                x = *maxim - *minim;
                fout<<x<<"\n";
            }
            else fout<<-1<<"\n";

        }
        else if(comanda == "MIN")
        {
            if(Z.size() >= 2)
            {
                auto it = Z.begin();
                int x = *it;
                it++;
                int y = *it;
                fout<< y - x <<"\n";
            }
            else fout<<-1<<"\n";
        }
    }
}