Pagini recente » Cod sursa (job #2539500) | Cod sursa (job #799243) | Cod sursa (job #1795286) | Cod sursa (job #590408) | Cod sursa (job #3135127)
#include <iostream>
#include <fstream>
#include <set>
#include <cmath>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
set<int> zeap;
void inserare(int x)
{
zeap.insert(x);
}
int stergere(int x)
{
if (zeap.count(x))
{
zeap.erase(x);
return 1;
}
return -1;
}
int cauta(int x)
{
return zeap.count(x);
}
int maxim()
{
if (zeap.size() < 2)
return -1;
return abs(*zeap.rbegin() - *zeap.begin());
}
int minim()
{
if (zeap.size() < 2)
return -1;
int minDif = INT_MAX;
auto it = zeap.begin();
int prev = *it;
++it;
while (it != zeap.end())
{
minDif = min(minDif, abs(*it - prev));
prev = *it;
++it;
}
return minDif;
}
int main()
{
string comanda;
int x;
while (fin >> comanda)
{
if (comanda == "I")
{
fin >> x;
inserare(x);
}
else if (comanda == "S")
{
fin >> x;
int val = stergere(x);
if (val == -1)
fout << -1 << "\n";
}
else if (comanda == "C")
{
fin >> x;
fout << cauta(x) << "\n";
}
else if (comanda == "MAX")
{
fout << maxim() << "\n";
}
else if (comanda == "MIN")
{
fout << minim() << "\n";
}
}
fin.close();
fout.close();
return 0;
}