Cod sursa(job #1033731)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 17 noiembrie 2013 14:58:52
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.81 kb
#include <set>

#include <cstdio>
#include <cstring>
using namespace std;

multiset < int > mySet, difSet;
char s[50];

inline int getValue(char s[], int len) {
    int ret = 0;
    for(int i = 0; i < len; ++i)
        if(s[i] >= '0' && s[i] <= '9') {
            while(i < len && s[i] >= '0' && s[i] <= '9')
                ret = ret * 10 + s[i] - '0', ++i;
            break;
        }
    return ret;
}

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

    multiset < int > :: iterator pos, pos1, pos2;

    while(!feof(stdin)) {
        fgets(s, 45, stdin);
        if(feof(stdin))
            break;
        int len = strlen(s);

        if(s[0] == 'I') {
            int temp = getValue(s, len);
            if(mySet.find(temp) == mySet.end()) {
                mySet.insert(temp);
                pos = mySet.find(temp);
                if(pos != mySet.begin()) {
                    pos1 = pos2 = pos;
                    --pos1, ++pos2;
                    difSet.insert(temp - *pos1);
                    if(pos2 != mySet.end()) {
                        difSet.erase(difSet.find(*pos2 - *pos1));
                        difSet.insert(*pos2 - temp);
                    }
                }
                else {
                    pos2 = pos;
                    ++pos2;
                    if(pos2 != mySet.end())
                        difSet.insert(*pos2 - temp);
                }
            }
        }
        else if(s[0] == 'S') {
            int temp = getValue(s, len);
            pos = mySet.find(temp);

            if(pos == mySet.end())
                printf("-1\n");
            else {
                pos1 = pos2 = pos;
                if(pos != mySet.begin()) {
                    --pos1;
                    difSet.erase(difSet.find(temp - *pos1));
                }
                ++pos2;
                if(pos2 != mySet.end())
                    difSet.erase(difSet.find(*pos2 - temp));
                if(pos != mySet.begin() && pos2 != mySet.end())
                    difSet.insert(*pos2 - *pos1);
                mySet.erase(pos);
            }
        }
        else if(s[0] == 'C') {
            int temp = getValue(s, len);
            pos = mySet.find(temp);

            if(pos == mySet.end())
                printf("0\n");
            else printf("1\n");
        }
        else if(s[1] == 'A') {
            if(mySet.size() < 2)
                printf("-1\n");
            else {
                int ans;
                pos = mySet.end();
                --pos;
                ans = *pos;
                pos = mySet.begin();
                ans -= *pos;

                printf("%d\n", ans);
            }
        }
        else {
            if(difSet.size() == 0)
                printf("-1\n");
            else printf("%d\n", *difSet.begin());
        }
    }

    return 0;
}