Cod sursa(job #3139522)

Utilizator cosmin1307Marinica George Cosmin cosmin1307 Data 29 iunie 2023 13:33:07
Problema Zeap Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <iostream>
#include <string.h>
#include <fstream>
#include <set>

using namespace std;

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

class Zeap
{
    set<int> multime;
    int min;
    int min2;
    int max;

public:
    Zeap()
    {
        this->multime = {};
        this->min = -1;
        this->min2 = -1;
        this->max = -1;
    }

    void insert(int x)
    {
        if (this->max == -1)
            this->max = x;
        else if (this->max < x)
            this->max = x;
        if (this->min == -1)
            this->min = x;
        else if (this->min > x)
            this->min = x;
        this->multime.insert(x);
    }
    int remove(int x)
    {
        auto found = this->multime.find(x);
        if (found != this->multime.end())
        {
            this->multime.erase(x);
            return 0;
        }
        else
            return -1;
    }
    bool search(int x)
    {
        auto found = this->multime.find(x);
        if (found != this->multime.end())
            return 1;
        else
            return 0;
    }
    int max_dif()
    {
        return this->max - this->min;
    }
    int min_dif()
    {
        int temp_min = 99999;
        for (int i = 0; i < sizeof(this->multime); i++)
        {
            auto it = this->multime.begin();
            advance(it, i);
            if (this->min < *it && *it < temp_min)
                temp_min = *it;
        }
        this->min2 = temp_min;
        return this->min2 - this->min;
    }
};

int main()
{
    Zeap zeap;
    string cmd;
    int x;
    while (in >> cmd)
    {
        if (cmd == "I")
        {
            in >> x;
            zeap.insert(x);
        }
        if (cmd == "S")
        {
            in >> x;
            if (zeap.remove(x) == -1)
                out << -1 << endl;
        }
        if (cmd == "C")
        {
            in >> x;
            out << zeap.search(x) << endl;
        }
        if (cmd == "MIN")
            out << zeap.min_dif() << endl;
        if (cmd == "MAX")
            out << zeap.max_dif() << endl;
    }
    in.close();
    out.close();
}