Cod sursa(job #1508609)

Utilizator BLz0rDospra Cristian BLz0r Data 22 octombrie 2015 19:20:43
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.18 kb
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
using namespace std;

FILE *f = fopen ( "zeap.in", "r" );
FILE *g = fopen ( "zeap.out", "w" );

set < int > Set;
map < int, int > Map;
priority_queue < int > Heap;
char s[20], *p;

int ReadInt (){

    while ( *p < '0' || *p > '9' )
        p++;

    int nr = 0;

    while ( *p >= '0' && *p <= '9' ){
        nr = nr * 10 + ( *p - '0' );
        p++;
    }

    return nr;
}

int main(){

    int x, val;
    set < int > :: iterator it, it1, it2;

    while ( fgets(s, 20, f) != NULL ){
        p = s;

        if ( s[0] == 'I' ){

            x = ReadInt();

            if ( Set.find(x) != Set.end() )
                continue;

            it = Set.insert( Set.begin(), x );

            it1 = it; it1--;
            it2 = it; it2++;

            if ( it != Set.begin() && it2 != Set.end() ){
                val = abs( *it2 - *it1 );
                Map[val]--;
            }

            if ( it != Set.begin() ){
                val = abs( *it - *it1 );
                Map[val]++;
                if ( Map[val] == 1 )
                    Heap.push(-val);
            }

            if ( it2 != Set.end() ){
                val = abs( *it2 - *it );
                Map[val]++;
                if ( Map[val] == 1 )
                    Heap.push(-val);
            }

            continue;
        }

        if ( s[0] == 'S' ){

            x = ReadInt();
            it = Set.find(x);

            if ( it != Set.end() ){

                it1 = it; it1--;
                it2 = it; it2++;

                if ( it != Set.begin() && it2 != Set.end() ){
                    val = abs( *it2 - *it1 );
                    Map[val]++;
                    if ( Map[val] == 1 )
                        Heap.push(-val);
                }

                if ( it != Set.begin() ){
                    val = abs( *it - *it1 );
                    Map[val]--;
                }

                if ( it2 != Set.end() ){
                    val = abs( *it2 - *it );
                    Map[val]--;
                }

                Set.erase(it);
            }
            else
                fprintf ( g, "-1\n" );

            continue;
        }

        if ( s[0] == 'C' ){

            x = ReadInt();

            if ( Set.find(x) != Set.end() )
                fprintf ( g, "1\n" );
            else
                fprintf ( g, "0\n" );

            continue;
        }

        if ( s[1] == 'A' ){

            if ( Set.size() < 2 )
                fprintf ( g, "-1\n" );
            else{
                it = Set.end();
                it--;
                fprintf ( g, "%d\n", abs ( *Set.begin() - *it ) );
            }
            continue;
        }
        if ( s[1] == 'I' ){

            if ( Set.size() < 2 )
                fprintf ( g, "-1\n" );
            else{
                while ( !Heap.empty() && Map[-Heap.top()] <= 0 )
                    Heap.pop();

                fprintf ( g, "%d\n", -Heap.top() );
            }

            continue;
        }
    }

    return 0;
}