Cod sursa(job #2751572)

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



struct pereche{
    int a, b;
    pereche(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }

    pereche& operator=(const pereche& per)
    {
        if(this != &per)
        {
            this->a = per.a;
            this->b = per.b;
        }
        return *this;
    }

    bool operator<(const pereche& per) const
    {
        return abs(this->a - this->b) < abs(per.a - per.b);
    }

    bool operator>(const pereche& per) const
    {
        return abs(this->a - this->b) > abs(per.a - per.b);
    }
};

priority_queue<pereche, vector<pereche>, greater<pereche>> PQ;

set<int> Z;
void inserare(int x)
{
    for(auto it = Z.begin(); it != Z.end(); it++)
    {
        pereche aux(x, *it);
        PQ.push(aux);
    }
    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)
            {
                while(Z.count(PQ.top().a) == 0 || Z.count(PQ.top().b) == 0)
                PQ.pop();
                pereche x = PQ.top();
                fout<<abs(x.a - x.b)<<"\n";
            }
            else fout<<-1<<"\n";

        }
    }
}