Cod sursa(job #2751726)

Utilizator andreinichitaTirziu Nichita andreinichita Data 15 mai 2021 18:05:54
Problema Zeap Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.69 kb
#include <fstream>
#include <set>
#include <cmath>
#include <queue>
using namespace std;
set<int>s;
string str;

class Compare
{
public:
    bool operator()(pair<int,int> p1, pair<int,int> p2)
    {
        return fabs(p1.first-p1.second)<fabs(p2.first-p2.second);
    }
};

priority_queue<pair<int,int>, vector<pair<int,int>>, Compare>minDif;

int main()
{
    ifstream cin("zeap.in");
    ofstream cout("zeap.out");
    int n,i,nr;
    getline(cin,str);
    while(str.size()>=2)
    {
        n=str.size();
        if(str[0]=='M')
        {
            if(s.size()<2)
            {
                cout<<"-1\n";
                continue;
            }
            if(str[1]=='A')
                cout<<*s.rbegin()-*s.begin()<<"\n";
            else
            {
                auto p=minDif.top();
                while(!minDif.empty())
                {
                    if(s.find(p.first)!=s.end()&&s.find(p.second)!=s.end())
                        break;
                    minDif.pop();
                    p=minDif.top();
                }
                if(minDif.empty())
                    cout<<"-1\n";
                else
                    cout<<fabs(p.first-p.second)<<"\n";
            }
        }
        else
        {
            nr=0;
            for(i=2; i<n; i++)
                nr=nr*10+str[i]-'0';
            if(str[0]=='I')
            {
                //insert and check neighbours
                s.insert(nr);
                auto it=s.find(nr);
                if(it!=s.begin())
                {
                    it--;
                    minDif.push(make_pair(nr,*it));
                    it++;
                }

                it++;
                if(it!=s.end())
                    minDif.push(make_pair(nr,*it));
            }
            else if(str[0]=='C')
            {
                auto it=s.find(nr);
                if(it!=s.end())
                    cout<<"1\n";
                else
                    cout<<"0\n";
            }
            else
            {
                //check neighbours and delete
                auto it=s.find(nr);
                if(it!=s.end())
                {
                    auto it1=it;
                    it1--;
                    if(it!=s.begin())
                    {
                        it++;
                        if(it!=s.end())
                            minDif.push(make_pair(*it1,*it));
                        it--;
                    }
                    s.erase(it);
                }
                else
                    cout<<"-1\n";
            }
        }
        getline(cin,str);
    }
    return 0;
}