Pagini recente » Cod sursa (job #2835593) | Cod sursa (job #2574871) | Cod sursa (job #1681236) | Cod sursa (job #2413003) | Cod sursa (job #339115)
Cod sursa(job #339115)
#include <cstdio>
#include <cstring>
#include <ctime>
#define file_in "zeap.in"
#define file_out "zeap.out"
#define Inf 0x3f3f3f3f
int nr,zeap[301000];
char s[100];
inline int abs(int a) { return a>=0?a:-a; }
void insert(int x)
{
int ok=0,i;
for (i=1;i<=nr && !ok;++i)
if (zeap[i]==x)
ok=1;
if (!ok)
zeap[++nr]=x;
}
int sterge(int x)
{
int ok=0,poz,i;
for (i=1;i<=nr && !ok;++i)
if (zeap[i]==x)
poz=i,
ok=1;
if (!ok)
return 1;
else
{
for (i=poz;i<=nr;++i)
zeap[i]=zeap[i+1];
nr--;
return 0;
}
}
int cauta(int x)
{
int ok=0,i;
for (i=1;i<=nr && !ok;++i)
if (zeap[i]==x)
ok=1;
return ok;
}
int max_dif()
{
int max=0,i,j;
for (i=1;i<nr;++i)
for (j=i+1;j<=nr;++j)
if (abs(zeap[i]-zeap[j])>max)
max=abs(zeap[i]-zeap[j]);
return max;
}
int min_dif()
{
int min=Inf,i,j;
for (i=1;i<nr;++i)
for (j=i+1;j<=nr;++j)
if (abs(zeap[i]-zeap[j])<min)
min=abs(zeap[i]-zeap[j]);
return min;
}
int main()
{
int i,x;
freopen(file_in,"r",stdin);
freopen(file_out,"w",stdout);
memset(zeap,0,sizeof(zeap));
nr=0;
while(gets(s))
{
//gets(s);
i=0;
if (s[0]=='I')//insert
{
i=2;
x=0;
while(s[i]>='0' && s[i]<='9')
{
x=x*10+s[i]-'0';
i++;
}
insert(x);
}
else
if (s[0]=='S')//sterge
{
i=2;
x=0;
while(s[i]>='0' && s[i]<='9')
{
x=x*10+s[i]-'0';
i++;
}
if (sterge(x))
printf("-1\n");
}
else
if (s[0]=='C')//cauta
{
i=2;
x=0;
while(s[i]>='0' && s[i]<='9')
{
x=x*10+s[i]-'0';
i++;
}
printf("%d\n", cauta(x));
}
else
if (s[0]=='M' && s[1]=='A')//max
{
if (nr<2)
printf("-1\n");
else
printf("%d\n", max_dif());
}
else
if (s[0]=='M' && s[1]=='I')//min
{
if (nr<2)
printf("-1\n");
else
printf("%d\n", min_dif());
}
}
fclose(stdin);
fclose(stdout);
return 0;
}