Cod sursa(job #1552794)

Utilizator akaprosAna Kapros akapros Data 18 decembrie 2015 17:51:20
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <stdio.h>
#include <string.h>

#include <set>
#include <map>

using namespace std;

char s[20];

set <int> h;
map <int, int> dif;

void insert (int x)
{
    if (h.find (x) != h.end())
        return;

    set <int> :: iterator it1, it2;
    it1 = h.lower_bound (x);
    it2 = it1;
    it2 --;

    int x1 = *it1, x2 = *it2;

    if (x1 - x2 >= 0)
        dif[x1 - x2] --;
    if (x1 - x >= 0)
        dif[x1 - x] ++;
    if (x - x2 >= 0)
        dif[x - x2] ++;
    h.insert (x);
}

int erase (int x)
{
    if (h.find(x) == h.end())
        return 1;

    set <int> :: iterator it1, it2;

    it1 = h.upper_bound (x);
    it2 = h.lower_bound (x);
    it2 --;

    h.erase (x);

    int x1 = *it1, x2 = *it2;

    if (x1 - x >= 0)
        dif[x1 - x] --;
    if (x - x2 >= 0)
        dif[x - x2] --;

    if (x1 - x2 >= 0)
        dif[x1 - x2] ++;

    return 0;
}

int calcmin ()
{
    if (h.size() >= 4)
        return dif.begin() -> first;
    return -1;
}

int calcmax ()
{
    if (h.size() >= 4)
    {
        set <int> :: iterator it1, it2;
        it1 = h.begin();
        it1 ++;
        it2 = h.end();
        it2 --;
        it2 --;
        return *it2 - *it1;
    }
    return -1;
}

inline int calc ()
{
    int x = 0, i;
    for (i = 3; s[i] >= '0' && s[i] <= '9'; i ++)
        x = x * 10 + s[i] - '0';
    return x;
}

int main ()
{
    freopen ("zeap.in", "r", stdin);
    freopen ("zeap.out", "w", stdout);

    h.insert (2000000005);
    h.insert (-1000000005);

    int nr = 0;
    while (gets (s + 1))
    {
        if (s[1] == 'I')
            insert (calc ());
        if (s[1] == 'S')
            if (erase (calc ()))
                printf ("-1\n");
        if (s[1] == 'C')
            printf ("%d\n", h.find(calc()) != h.end());
        if (s[1] == 'M')
            if (s[2] == 'I')
                printf ("%d\n", calcmin());
            else
                printf ("%d\n", calcmax());
    }
    return 0;
}