Cod sursa(job #652002)

Utilizator indestructiblecont de teste indestructible Data 22 decembrie 2011 18:11:25
Problema Zeap Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <stdio.h>
#include <string.h>
#include <set>
#define LMAX 51
using namespace std;
char line[LMAX];
int poz,nr;
inline int cif(char x)
{
	return x>='0' && x<='9'; 
}
void read()
{
	while (!cif(line[poz+1])) poz++;
	while (cif(line[poz+1])){poz++; nr=nr*10+line[poz]-'0';}
}
set <int> A;
set <int> :: iterator it1,it2,it3;
multiset <int> B;
multiset <int> :: iterator it;
int main()
{
	freopen("zeap.in","r",stdin);
	freopen("zeap.out","w",stdout);
	while (!feof(stdin))
	{
		memset(line,0,sizeof(line));
		fgets(line+1,LMAX,stdin);
		poz=0; nr=0;
		if (line[1]=='I')
		{
			read();
			if (A.find(nr)==A.end())
			{
				A.insert(nr);
				it1=A.find(nr); it2=it1; it2++;
				if (it2!=A.end())
					B.insert((*it2)-(*it1));
				if (it1!=A.begin())
				{
					it2=it1; it2--;
					B.insert((*it1)-(*it2));
				}
			}
		}
		if (line[1]=='S')
		{
			read();
			it1=A.find(nr);
			if (A.find(nr)==A.end())
				printf("-1\n");
			else
			{
				it2=it1; it2++; it3=it1;
				if (it2!=A.end())
				{
					B.erase((*it2)-(*it1));
				}
				if (it1!=A.begin())
				{
					it3--;
					B.erase((*it1)-(*it3));
				}
				if (it2!=A.end() && it1!=A.begin())
					B.insert((*it2)-(*it3));
				A.erase(it1);
			}
		}
		if (line[1]=='C')
		{
			read();
			it1=A.find(nr);
			if (it1!=A.end())
				printf("1\n");
			else
				printf("0\n");
		}
		if (line[1]=='M' && line[2]=='A')
		{
			if (A.empty())
				printf("-1\n");
			else
			{
				it1=A.begin(); it1++;
				if (it1==A.end())
					printf("-1\n");
				else
				{
					it1--;
					it2=A.end(); it2--;
					printf("%d\n",(*it2)-(*it1));
				}
			}
		}
		if (line[1]=='M' && line[2]=='I')
		{
			if (B.empty())
				printf("-1\n");
			else
			{
				it=B.begin();
				printf("%d\n",(*it));
			}
		}
	}
	return 0;
}