Cod sursa(job #2751721)

Utilizator andreea_07Andreea Georgescu andreea_07 Data 15 mai 2021 17:55:23
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <bits/stdc++.h>

using namespace std;

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


struct perechi   ///perechi vecine
{   int st,dr;
    perechi(int x, int y)
    {st = x;
    dr = y;
    }
};


struct comparare ///criteriul de adaugare in coada cu prioritati
{

    bool operator()  ( perechi x, perechi y)
    {
        return abs(x.st - x.dr) > abs(y.st - y.dr);
    }
};

priority_queue<perechi, vector<perechi>, comparare> coada;

int inserare(set<int> &M,int x)
{
    if(!M.count(x))
    {
        M.insert(x);
        return 1;
    }

    return -1;

}

int stergere(set<int> &M,int x)
{
    if(M.count(x))
    {
        set<int>::iterator it = M.find(x); ///pozitia elementului ce trebuie sters

        set<int>::iterator it1 = it;

        it1++;  ///pozitia vecinului din dreapta

        if(it != M.begin() && it1 != M.end())
        {
            --it; /// pozitia vecinului din stanga
            coada.push(perechi(*it, *it1));
        }

        M.erase(x);
        return 1;
    }


    return -1;
}


int cautare(set<int> &M,int x)
{
    if(M.count(x)!=0)
        return 1;


 return 0;
}


int max_dif(set<int> &M) /// diferenta dintre primul element(cel mai mic) si ultimul()cel mai mare
{

    if(M.size() < 2)
        return -1;
    else
    {
        set<int>::iterator  u = M.end(),p = M.begin();

        u--;

        return *u - *p;
    }
}


int min_dif(set<int> &M)
{
    if(M.size() < 2)
        return -1;

    else
    {

        while(M.count(coada.top().dr)==0 || M.count(coada.top().st)==0 )

            coada.pop();

        return abs( coada.top().dr-coada.top().st );
    }
}


int main()
{
    set<int> M;
    string comanda;
    int x;

    while(fin>>comanda)
    {
        if(comanda == "I")
        {
            fin>>x;

            if(inserare(M, x) == 1)
            {
                set<int>::iterator it = M.find(x); /// pozitia lui x

                if(it != M.begin())
                {
                    set<int>::iterator it1 = it; ///pozitia vecinului stang
                    --it1;
                    coada.push(perechi(*it1, x));

                }

                set<int>::iterator it2 = it; ///pozitia vecinului  drept
                ++it2;
                if(it2 != M.end())
                {
                    coada.push(perechi(x, *it2));
                }
            }
        }
             else if(comanda == "S")
          {
            fin>>x;

            if(stergere(M, x) == -1)
                fout<< "-1\n";
        }
        else if(comanda == "C")
        {
            fin>>x;
            fout<<cautare(M, x) << "\n";
        }

        else if(comanda == "MAX")
        {
            fout<<max_dif(M)<<"\n";
        }
        else
        {
            fout<<min_dif(M)<<"\n";
        }
    }



    return 0;
}