Cod sursa(job #1807835)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 16 noiembrie 2016 22:47:12
Problema Zeap Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.48 kb
#include <iostream>
#include <cstdio>
#include <set>
#include <map>

using namespace std;

char buf[30];
set<int> data;
map<int, int> dif;
set<int>::iterator it1, it2, it3;

void debug()
{
    for (auto it = dif.begin(); it != dif.end(); ++it)
        cerr << it->first << ":" << it->second << " ";
    cerr << "\n";
}

inline void delOne(map<int, int> &s, int val)
{
    --s[val];
}

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

    while (cin.getline(buf, 30))
    {
        if (buf[0] == 'M') {
            if (buf[1] == 'A') {
                if (data.size() < 2) {
                    printf("-1\n");
                    continue;
                }
                it1 = data.begin();
                it2 = data.end(); it2--;
                printf("%d\n", *it2 - *it1);
            }
            else {
                if (data.size() < 2) {
                    printf("-1\n");
                    continue;
                }
                auto ax = dif.begin();
                while (ax->second == 0) {
                    auto ax2 = ax;
                    ax2++;
                    dif.erase(ax);
                    ax = ax2;
                }
                printf("%d\n", dif.begin()->first);
            }
        }
        else {
            int nr = 0;
            for (int i = 2; buf[i]; i++)
                nr = nr*10 + buf[i]-'0';
            if (buf[0] == 'I') {
                pair<set<int>::iterator, bool> p = data.insert(nr);
                if (p.second == true) {
                    it1 = p.first;
                    if (data.size() == 1) continue;
                    if (it1 == data.begin()) {
                        ++it1;
                        dif[*it1 - *p.first]++;
                        continue;
                    }
                    it2 = data.end(); it2--;
                    if (it1 == it2) {
                        --it1;
                        dif[*it2 - *it1]++;
                        continue;
                    }
                    it2 = it1; it1--;
                    it3 = it1; it3++;
                    delOne(dif, *it3-*it1);
                    dif[*it2 - *it1]++;
                    dif[*it3 - *it2]++;
                }
            }
            else if (buf[0] == 'S') {
                it1 = data.find(nr);
                if (it1 == data.end()) {
                    printf("-1\n");
                    continue;
                }
                if (data.size() == 1) {
                    data.erase(it1);
                    continue;
                }
                it2 = data.begin(), it3 = data.end();
                --it3;
                if (it1 == it2) {
                    it2++;
                    delOne(dif, *it2 - *it1);
                    data.erase(it1);
                    continue;
                }
                if (it1 == it3) {
                    it1--;
                    delOne(dif, (*it3 - *it1));
                    data.erase(it3);
                    continue;
                }
                it2 = it3 = it1;
                it1--, it3++;
                dif[*it3 - *it1]++;
                delOne(dif, *it2 - *it1);
                delOne(dif, *it3 - *it2);
                data.erase(it2);
            }
            else if (buf[0] == 'C') {
                printf("%d\n", data.find(nr) != data.end());
            }
        }
        //debug();
    }

    return 0;
}