Cod sursa(job #2899627)

Utilizator bianca2002Bianca bianca2002 Data 8 mai 2022 23:39:56
Problema Zeap Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <iostream>
#include <set>
#include <queue>
#include <fstream>

using namespace std;

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

priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>v;

set<int>z;

void adauga(int x)
{
    if(z.count(x)==0)
    {
        z.insert(x);
        if(z.size()>=2)
        {
            auto indice = z.find(x);
            if(indice != z.begin())
            {
                auto st =indice;
                st--;
                v.push({abs(*st-x),{*st,x}});
            }
            auto dr = indice;
            dr++;
            if(dr!=z.end())
                v.push({abs(x-*dr),{x,*dr}});
        }
    }
}

int elimina(int x)
{
    if(z.count(x)==0)
        return -1;
    auto indice = z.find(x);
    auto dr = indice;
    dr++;
    auto st = indice;
    st--;
    if(dr!=z.end() && indice!=z.begin())
        v.push({abs(*st- *dr),{*st,*dr}});
    z.erase(x);
    return 1;
}

int gaseste(int x)
{
    if(z.count(x))
        return 1;
    return 0;
}

int diferenta1()
{
    if(z.size()<2)
        return -1;
    auto j = z.end();
    j--;
    auto i = z.begin();
    return *j- *i;
}

int diferenta2()
{
    if(z.size()<2)
        return -1;
    while(z.count(v.top().second.first)==0 || z.count(v.top().second.second)==0)
        v.pop();
    return v.top().first;
}

int main()
{
    string a;
    int x;
    while(f>>a)
        if(a=="I")
        {
            f>>x;
            adauga(x);
        }
        else if(a=="S")
        {
            f>>x;
            int y = elimina(x);
            if(y == -1)
                g<<-1<<endl;
        }
        else if(a=="C")
        {
            f>>x;
            g<<gaseste(x)<<endl;
        }
        else if(a=="MAX")
            g<<diferenta1()<<endl;
        else
            g<<diferenta2()<<endl;
}