Cod sursa(job #1847201)

Utilizator mariakKapros Maria mariak Data 14 ianuarie 2017 13:21:35
Problema Zeap Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <bits/stdc++.h>
FILE *fin  = freopen("zeap.in", "r", stdin);
FILE *fout = freopen("zeap.out", "w", stdout);

using namespace std;
set <int> S;
multiset <int> Dif;
char s[20];

int form_nr()
{
    int n = strlen(s), ans = 0;
    for(int i = 2; i < n; ++ i)
        ans = ans * 10 + s[i] - '0';
    return ans;
}

int main()
{
    gets(s);
    while(s[0] != '0')
    {
        if(s[0] == 'I')
        {
            set<int>::iterator it1 = S.end(), it2 = S.end();
            int value = form_nr();
            if (S.find(value) != S.end())
                continue;
            S.insert(value);
            set<int>::iterator i = S.find(value);
            if(i != S.begin())
            {
                set<int>::iterator j = i;
                j --;
                it1 = j;
                Dif.insert(value - *j);
            }
            set<int>::iterator k = S.end();
            k --;
            if(i != k)
            {
                set<int>::iterator j = i;
                ++ j;
                it2 = j;
                Dif.insert(*j - value);
            }

            if (it1 != S.end() && it2 != S.end())
                Dif.erase(*it2 - *it1);
        }
        if(s[0] == 'S')
        {
            int value = form_nr();
            set<int>::iterator i = S.find(value);
            if(i == S.end())
                printf("-1\n");
            else
            {
                set<int>::iterator it1 = S.end(), it2 = S.end();
                if(i != S.begin())
                {
                    set<int>::iterator j = i;
                    j --;
                    it1 = j;
                    Dif.erase(value - *j);
                }
                set<int>::iterator k = S.end();
                -- k;
                if(i != k)
                {
                    set<int>::iterator j = i;
                    j ++;
                    it2 = j;
                    Dif.erase(*j - value);
                }
                if (it1 != S.end() && it2 != S.end())
                    Dif.insert(*it2 - *it1);
                S.erase(i);
            }
        }
        if(s[0] == 'C')
        {
            int value = form_nr();
            set<int>::iterator i = S.find(value);
            if(i == S.end())
                printf("0\n");
            else printf("1\n");
        }

        if(s[0] == 'M' && s[1] == 'I')
        {
            if(S.size() < 2)
                printf("-1\n");
            else printf("%d\n", *(Dif.begin()));
        }
        if(s[0] == 'M' && s[1] == 'A')
        {
            if(S.size() < 2)
                printf("-1\n");
            else
            {
                set<int>::iterator i = S.end();
                -- i;
                set<int>::iterator j = S.begin();
                printf("%d\n", *i - *j);
            }
        }
        s[0] = '0';
        gets(s);
    }
    return 0;
}