Pagini recente » Cod sursa (job #71011) | Cod sursa (job #239723) | Cod sursa (job #70704) | Cod sursa (job #3141782) | Cod sursa (job #3135133)
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
unordered_set<int> zeap;
vector<int> sortedZeap;
void inserare(int x)
{
if (zeap.count(x) == 0)
{
zeap.insert(x);
sortedZeap.push_back(x);
sort(sortedZeap.begin(), sortedZeap.end());
}
}
int stergere(int x)
{
if (zeap.count(x) == 1)
{
zeap.erase(x);
sortedZeap.erase(find(sortedZeap.begin(), sortedZeap.end(), x));
return 1;
}
return -1;
}
int cauta(int x)
{
if (zeap.count(x) == 1)
{
return 1;
}
return 0;
}
int maxDif()
{
if (sortedZeap.size() < 2)
{
return -1;
}
return sortedZeap.back() - sortedZeap.front();
}
int minDif()
{
if (sortedZeap.size() < 2)
{
return -1;
}
int minDiff = INT_MAX;
for (int i = 1; i < sortedZeap.size(); i++)
{
minDiff = min(minDiff, sortedZeap[i] - sortedZeap[i - 1]);
}
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;
}