Pagini recente » Cod sursa (job #3001294) | Cod sursa (job #2713888) | Cod sursa (job #3156179) | Cod sursa (job #567904) | Cod sursa (job #3135136)
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <climits>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
unordered_set<int> zeap;
void inserare(int x)
{
zeap.insert(x);
}
int stergere(int x)
{
if (zeap.erase(x))
return 1;
return -1;
}
int cauta(int x)
{
return zeap.count(x);
}
int maxDif()
{
if (zeap.size() < 2)
return -1;
int maxDiff = INT_MIN;
for (int num : zeap)
{
for (int otherNum : zeap)
{
if (num != otherNum)
{
maxDiff = max(maxDiff, abs(num - otherNum));
}
}
}
return maxDiff;
}
int minDif()
{
if (zeap.size() < 2)
return -1;
int minDiff = INT_MAX;
for (int num : zeap)
{
for (int otherNum : zeap)
{
if (num != otherNum)
{
minDiff = min(minDiff, abs(num - otherNum));
}
}
}
return minDiff;
}
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 << maxDif() << "\n";
}
else if (comanda == "MIN")
{
fout << minDif() << "\n";
}
}
fin.close();
fout.close();
return 0;
}