Cod sursa(job #988626)

Utilizator DaNutZ2UuUUBB Bora Dan DaNutZ2UuU Data 23 august 2013 15:00:26
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.35 kb
#include<fstream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<set>
 
#define NMAX 25
#define get_min(a,b) ((a)<(b)?(a):(b))
 
using namespace std; 
 
ifstream f("zeap.in");
ofstream g("zeap.out");
 
char line[NMAX];
 
priority_queue<int,vector<int>,greater<int> >  HEAP ,DA ;
set < int >  AVL;
int X , type ; 
 
inline void FindType ( void )
{
    bool ok = false ;
    if( line[0] == 'I' )
        type = 1 ;
    if( line[0] == 'S' )
        type = 2 ;
    if( line[0] == 'C' )
        type = 3 ;
    if( line[0] == 'M')
    {
        ok = true ; 
        if( line[1] == 'I' )
                type = 5 ; 
           else
                type = 4 ;
    }
    if ( ok == false )
    {
        for( int i(2) ; line[i] ; ++i )
            X=X*10+line[i]-'0';
    }
}
set < int > :: iterator it1 , it2 , it3;
int main ( void )
{
     
    while ( f.getline(line,25) )
    {
        X = 0 ;
        FindType();
        if( type == 1 )
        {
        pair < set<int>::iterator , bool > ok ;
        ok = AVL.insert(X) ;
        if( ok.second == 0 )
            continue ;
        if( AVL.size () == 2  )
        {
          it1=it2=AVL.begin();
           ++it2;         
          HEAP.push(*it2-*it1);
          continue ;
        }
        if( AVL.size()  <= 2 ) 
            continue ;
        it3 = ok.first ;
        if( it3 == AVL.begin () )
        {
            it1 = it3 ;
            it2 = ++ it1  ;
            HEAP.push( *it2 - *it3 );
            continue ;
        }
         
        it2 = it3++ ;
        it1 = --it2 ;
        ++it2;
        if( it3 == AVL.end() )
        {
             
            HEAP.push(*it2-*it1);
            continue;
        }
         
        HEAP.push(get_min( (*it3-*it2),(*it2-*it1)));
         
        continue ; 
        }
        else
            if( type == 2 )
            {
                if( AVL.empty() )
                {
                    g<<-1<<"\n";
                    continue;
                }
                it1 = AVL.end() ; 
                --it1 ; 
                it2 = AVL.find(X);
                if( it2 != AVL.end() )
                {
                    if( AVL.size() == 1 )
                    {
                        AVL.erase(it2);
                        continue ;
                    }
                    if( it2 == it1 )
                    {
                        it3 = --it2 ;
                        ++it2 ;
                        DA.push(*it2-*it3);
                        AVL.erase(it2);
                        continue ; 
                    }
                    if( it2 == AVL.begin() )
                    {
                        it3 = ++it2 ; 
                        --it2 ;
                        DA.push(*it3-*it2);
                        AVL.erase(it2);
                        continue ; 
                         
                    }
                    it3 = ++it2;
                    --it2;
                    it1 = --it2 ;
                    ++it2;
                    DA.push(*it2-*it1);
                    DA.push(*it3-*it2);
                    HEAP.push(*it3-*it1);
                    AVL.erase(it2);
                    continue ;
                }
                else
                {
                    g<<-1<<"\n";
                    continue;
                }
            }
        else
            if( type == 3 )
                g<<(AVL.find(X)!=AVL.end())<<"\n";
        else
            if( type == 4 )
            {
                if( AVL.size() < 2 )
                {
                    g<<-1<<"\n";
                    continue;
                }
                it1 = AVL.end() ; 
                --it1;
                it2 = AVL.begin();
             g<<*it1-*it2<<"\n";
            }
 
            else
                if( type == 5 )
                {
                    if( AVL.size() < 2 )
                {
                    g<<-1<<"\n";
                    continue;
                }
                    while ( !DA.empty() && DA.top() == HEAP.top() )
                    {
                                HEAP.pop();
                                DA.pop();
                    }
          g<<HEAP.top()<<"\n";
                }
    }
    f.close();
    g.close();
    return 0 ;
}