Cod sursa(job #1741877)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 15 august 2016 13:15:48
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.78 kb
#include <cstdio>
#include <cctype>
#include <set>
using namespace std;
set <int> s;
set <int> :: iterator it1;
multiset <int> dif;
multiset <int> :: iterator it2;
FILE*fi,*fout;
char c;
inline int getnr(){
    int nr=0;
    while(isdigit(c)==1){
       nr=nr*10+c-'0';
       c=fgetc(fi);
    }
    return nr;
}
int main(){
   int a,b,x,con;
   fi=fopen("zeap.in" ,"r");
   fout=fopen("zeap.out" ,"w");
   c='a';
   while(c!=EOF){
      c=fgetc(fi);
      if(c=='I'){
          c=fgetc(fi);
          c=fgetc(fi);
          x=getnr();
          if(s.find(x)==s.end()){
             s.insert(x);
             it1=s.find(x);
             con=0;
             if(it1!=s.begin()){
                it1--;
                dif.insert(x-*it1);
                a=*it1;
                it1++;
                con++;
             }
             it1++;
             if(it1!=s.end()){
                dif.insert(*it1-x);
                b=*it1;
                con++;
             }
             if(con==2){
                 it2=dif.find(b-a);
                 if(it2!=dif.end())
                   dif.erase(it2);
             }
          }
      }
      if(c=='S'){
          c=fgetc(fi);
          c=fgetc(fi);
          x=getnr();
          it1=s.find(x);
          if(it1==s.end())
            fprintf(fout,"-1\n");
          else{
             con=0;
             if(it1!=s.begin()){
                 it1--;
                 a=*it1;
                 it2=dif.find(x-*it1);
                 if(it2!=dif.end())
                   dif.erase(it2);
                 it1++;
                 con++;
             }
             it1++;
             if(it1!=s.end()){
                b=*it1;
                it2=dif.find(*it1-x);
                if(it2!=dif.end())
                  dif.erase(it2);
                con++;
             }
             it1--;
             s.erase(it1);
             if(con==2)
               dif.insert(b-a);
          }
      }
      if(c=='C'){
         c=fgetc(fi);
         c=fgetc(fi);
         x=getnr();
         it1=s.find(x);
         if(it1==s.end())
            fprintf(fout,"0\n");
         else
            fprintf(fout,"1\n");
      }
      if(c=='M'){
         c=fgetc(fi);
         if(c=='A'){
            c=fgetc(fi);
            c=fgetc(fi);
            if(s.size()<2)
              fprintf(fout,"-1\n");
            else{
              it1=s.end();
              it1--;
              a=*it1;
              it1=s.begin();
              b=*it1;
              fprintf(fout,"%d\n" ,a-b);
            }
         }
         else{
            c=fgetc(fi);
            c=fgetc(fi);
            if(dif.size()==0)
               fprintf(fout,"-1\n");
            else{
               it2=dif.begin();
               fprintf(fout,"%d\n" ,*it2);
            }
         }
      }
   }
   fclose(fi);
   fclose(fout);
   return 0;
}