Cod sursa(job #1194174)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 3 iunie 2014 01:34:42
Problema Zeap Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.2 kb
#include<fstream>
#include<set>
using namespace std;
ifstream fi("zeap.in");
ofstream fo("zeap.out");

const int inf = (1LL<<31)-1;

multiset <int> m_set;
set <int>::iterator it;
set <int> s;
int x,a,b;
char c;

void inserare(){

     fi>>x;
     if(s.count(x)==0){
                       s.insert(x);
                       it=s.find(x);
                       it--;
                       a=*it;
                       it++;
                       it++;
                       b=*it;
                       
                       if(a>0) m_set.insert(x-a);
                       if(b<inf) m_set.insert(b-x);
                       if(a>0 && b<inf) m_set.insert(b-a);
                      }
}

void stergere(){
     
     fi>>x;
     if(s.count(x)!=0){
                       it=s.find(x);
                       it--;
                       a=*it;
                       it++;
                       it++;
                       b=*it;
                       it--;
                       
                       if(a>0) m_set.erase(m_set.find(x-a));
                       if(b<inf) m_set.erase(m_set.find(b-x));
                       if(a>0 && b<inf) m_set.insert(b-a);
                       
                       s.erase(it);
                      }
     else fo<<"-1\n";
}

void cautare(){

     fi>>x;
     if(s.count(x)!=0) fo<<"1\n";
     else fo<<"0\n";
}

void max_dif(){
     
     if(s.size()<=3) fo<<"-1\n";
     else{
          it=s.end();
          it--;
          it--;
          a=*it;
          it=s.begin();
          it++;
          b=*it;
          
          fo<<a-b<<"\n";
         }
}

void min_dif(){
     if(s.size()<=3) fo<<"-1\n";
     else fo<<*(m_set.begin())<<"\n";
}

int main(){
    s.insert(0);
    s.insert(inf);
    
    while(fi>>c){
                 if(c=='I') inserare();
                 else if(c=='S') stergere();
                 else if(c=='C') cautare();
                 else{
                      fi>>c>>c;
                      if(c=='X') max_dif();
                      else if(c=='N') min_dif();
                     }
                }
    
    fi.close();
    fo.close();
    return 0;
}