Pagini recente » Cod sursa (job #569521) | Cod sursa (job #2559899) | Cod sursa (job #342031) | Cod sursa (job #731450) | Cod sursa (job #2756126)
#include "bits/stdc++.h"
#include <cassert>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using pd = pair<double, double>;
using pld = pair<ld, ld>;
int main()
{
ifstream cin("zeap.in");
ofstream cout("zeap.out");
auto has_at_least_2_elems = [](set<int> &nrs) -> bool
{
if (nrs.empty())
{
return false;
}
int aux = *begin(nrs);
if (nrs.erase(begin(nrs)) == end(nrs))
{
nrs.emplace(aux);
return false;
}
nrs.emplace(aux);
return true;
};
set<int> nrs;
string op;
while (cin >> op)
{
if (op == "I")
{
int x;
cin >> x;
nrs.emplace(x);
}
else if (op == "S")
{
int x;
cin >> x;
auto it = nrs.find(x);
if (it == end(nrs))
{
cout << "-1\n";
}
else
{
nrs.erase(it);
}
}
else if (op == "C")
{
int x;
cin >> x;
cout << nrs.count(x) << "\n";
}
else if (op == "MAX")
{
if (has_at_least_2_elems(nrs))
{
cout << *prev(end(nrs)) - *begin(nrs) << "\n";
}
else
{
cout << "-1\n";
}
}
else // op = "MIN".
{
if (!has_at_least_2_elems(nrs))
{
cout << "-1\n";
continue;
}
auto beg_it1{begin(nrs)}, beg_it2{next(begin(nrs))}, end_it1{prev(end(nrs))}, end_it2{prev(prev(end(nrs)))};
cout << min(*beg_it2 - *beg_it1, *end_it1 - *end_it2) << "\n";
}
}
}