Cod sursa(job #752387)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 28 mai 2012 15:41:39
Problema Zeap Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <stdio.h>
#include <string.h>

#include <set>

using namespace std;

char s[20];

set <int> dif, h;

void insert (int x)
{
	if (h.find (x) != h.end())
		return;
	
	set <int> :: iterator it1, it2;
	it2 = it1 = h.lower_bound (x);
	it2 --;
	
	if (*it1 - *it2 >= 0)
		dif.erase (*it1 - *it2);
	dif.insert (*it1 - x);
	dif.insert (x - *it2);
	h.insert (x);
}

int erase (int x)
{
	if (h.find(x) == h.end())
		return 1;
	
	set <int> :: iterator it1, it2;
	
	it1 = h.upper_bound (x);
	it2 = h.lower_bound (x);
	it2 --;
	
	h.erase (x);
	
	dif.erase (*it1 - x);
	dif.erase (x - *it2);
	
	if (*it1 - *it2 >= 0)
		dif.insert (*it1 - *it2);
	
	return 0;
}

int calcmin ()
{
	if (h.size() >= 4)
		return *(dif.begin());
	return -1;
}

int calcmax ()
{
	if (h.size() >= 4)
	{
		set <int> :: iterator it1, it2;
		it1 = h.begin();
		it1 ++;
		it2 = h.end();
		it2 --;
		it2 --;
		return *it2 - *it1;
	}
	return -1;
}

inline int calc ()
{
	int x = 0, i;
	for (i = 3; s[i] >= '0' && s[i] <= '9'; i ++)
		x = x * 10 + s[i] - '0';
	return x;
}

int main ()
{
	freopen ("zeap.in", "r", stdin);
	freopen ("zeap.out", "w", stdout);
	
	h.insert (2000000005);
	h.insert (-1000000005);
	
	while (gets (s + 1))
	{
		if (s[1] == 'I')
			insert (calc ());
		if (s[1] == 'S')
			if (erase (calc ()))
				printf ("-1\n");
		if (s[1] == 'C')
			printf ("%d\n", h.find(calc()) != h.end());
		if (s[1] == 'M')
			if (s[2] == 'I')
				printf ("%d\n", calcmin());
			else
				printf ("%d\n", calcmax());
	}
	return 0;
}