Cod sursa(job #3135131)

Utilizator LazarDanielGabrielLazar Daniel-Gabriel LazarDanielGabriel Data 1 iunie 2023 22:02:45
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.06 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
using namespace std;

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

vector<int> zeap;
int maxDif = -1;
int minDif = -1;

void inserare(int x)
{
    auto it = lower_bound(zeap.begin(), zeap.end(), x);

    if (it == zeap.end() || *it != x)
    {
        zeap.insert(it, x);

        if (zeap.size() >= 2)
        {
            int diff = abs(zeap.back() - zeap.front());
            if (diff > maxDif)
                maxDif = diff;

            if (minDif == -1 || diff < minDif)
                minDif = diff;
        }
    }
}

int stergere(int x)
{
    auto it = lower_bound(zeap.begin(), zeap.end(), x);

    if (it != zeap.end() && *it == x)
    {
        zeap.erase(it);

        if (zeap.size() >= 2)
        {
            int diff = abs(zeap.back() - zeap.front());
            if (diff > maxDif)
                maxDif = diff;

            if (zeap.size() == 1)
                minDif = -1;
            else
                minDif = *zeap.rbegin() - *zeap.begin();
        }

        return 1;
    }

    return -1;
}

int cauta(int x)
{
    auto it = lower_bound(zeap.begin(), zeap.end(), x);

    if (it != zeap.end() && *it == x)
        return 1;

    return 0;
}

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;
}