Cod sursa(job #751953)

Utilizator PetcuIoanPetcu Ioan Vlad PetcuIoan Data 27 mai 2012 15:04:50
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.1 kb
#include<stdio.h>
#include<string.h>
#include<assert.h>

#include<set>
#include<string>
#include<iostream>
#include<functional>
#include<queue>
#include<vector>
#include<algorithm>

using namespace std;

multiset<int> t;
multiset<int, greater<int> > p;
priority_queue<int, vector<int>, greater<int> > h, bad;

int zerase(int x){
  if(t.find(x) == t.end())
    return 0;

  t.erase(x);
  p.erase(x);

  if(t.count(x) >= 2){
    bad.push(0);
    return 1;
  }

  multiset<int>::iterator itlow = p.upper_bound(x), itup = t.upper_bound(x);

  bad.push(x - *itlow);
  bad.push(*itup - x);
  h.push(*itup - *itlow);

  return 1;
}

int zfind(int x){
  if(t.find(x) == t.end())
    return 0;
  return 1;
}

void zinsert(int x){
  multiset<int>::iterator itlow = p.upper_bound(x), itup = t.upper_bound(x);

  if(*itup - *itlow > 0)
    bad.push(*itup - *itlow);
  h.push(*itup - x);
  h.push(x - *itlow);
  t.insert(x);
  p.insert(x);
}

int get_min(){
  if(t.size() < 4)
    return -1;

  while(!bad.empty() && h.top() == bad.top()){
    h.pop();
    bad.pop();
  }

  return h.top();
}

int get_max(){
  if(t.size() < 4)
    return -1;

  multiset<int>::iterator itlow = t.upper_bound(-9001), ithigh = p.lower_bound(1000000001);
  return *ithigh - *itlow;
}

int aux;
char qry[20];

void get_aux(){
  int lim = strlen(qry);

  aux = 0;
  for(int i = 2; i < lim; ++i)
    aux = aux * 10 + qry[i] - '0';
}

int main(){
  assert(freopen("zeap.in", "r", stdin));
  assert(freopen("zeap.out", "w", stdout));

  t.insert(-1000000005);
  t.insert(2000000005);
  p.insert(-1000000005);
  p.insert(2000000005);

  while(gets(qry)){
    if(qry[0] == 'M')
      if(qry[1] == 'I')
        printf("%d\n", get_min());
      else
        printf("%d\n", get_max());
    else if(qry[0] == 'I'){
      /*int aux;
      scanf("%d", &aux);*/
      get_aux();

      zinsert(aux);
    }
    else if(qry[0] == 'S'){
      /*int aux;
      scanf("%d", &aux);*/
      get_aux();

      if(!zerase(aux))
        printf("-1\n");
    }
    else{
      /*int aux;
      scanf("%d", &aux);*/
      get_aux();

      printf("%d\n", zfind(aux));
    }

    scanf("\n");
  }

  return 0;
}