Cod sursa(job #2899660)

Utilizator bianca2002Bianca bianca2002 Data 9 mai 2022 00:29:10
Problema Zeap Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.59 kb
#include <fstream>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cmath>

using namespace std;

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


struct ppair
{
    int s,d;
    ppair() {}
    ppair(int x, int y)
    {
        s = x;
        d = y;
    }
};


int insereaza(set<int> &z ,int x)
{
    if(!z.count(x))
    {
        z.insert(x);
        return 1;
    }
    else return -1;

}
class cmp
{
public:
    bool operator () (const ppair& a, const ppair& b)
    {
        return abs(a.s - a.d) > abs(b.s - b.d);
    }
};

priority_queue<ppair, vector<ppair>, cmp> minheap;

set<int> z;

int sterge(set<int> &z,int x)
{
    if(z.count(x))
    {
        auto i = z.find(x);
        auto j = i;
        j++;
        if(i != z.begin() && j != z.end())
        {
            i--;
            minheap.push(ppair(*i, *j));
        }
        z.erase(x);
        return 1;
    }
    else
    return -1;
}



int cauta(set<int> &z,int x)
{
    if(z.count(x))
        return 1;
    else
        return 0;
}


int max_dif(set<int> &z)
{

    if(z.size() < 2)
        return -1;
    else
    {
        auto p = z.begin(), u = z.end();
        u--;
        return *u - *p;
    }
}


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

        while(!z.count(minheap.top().d) || !z.count(minheap.top().s) )
        {
            minheap.pop();
        }
        return abs(minheap.top().s - minheap.top().d);
    }
}


int main()
{

    string s;
    int x;

    while(f >> s)
    {
        if(s == "I")
        {
            f >> x;
            int ok = insereaza(z, x);
            if(ok == 1)
            {
                auto i = z.find(x);

                if(i != z.begin())
                {
                    auto j = i;
                    j--;
                    minheap.push(ppair(*j, x));
                }

                auto k = i;
                k++;
                if(k != z.end())
                {
                    minheap.push(ppair(x, *k));
                }
            }
        }
        else if(s == "C")
        {
            f >> x;
            g << cauta(z, x) << endl;
        }
        else if(s == "S")
        {
            f >> x;
            int ok = sterge(z, x) ;
            if(ok == -1)
                g << -1<<endl;
        }
        else if(s == "MIN")
        {
            g << min_dif(z) << endl;
        }
        else
        {
            g << max_dif(z) << endl;
        }
    }
}