Pagini recente » Cod sursa (job #1110951) | Monitorul de evaluare | Cod sursa (job #2328575) | Cod sursa (job #1433002) | Cod sursa (job #2751863)
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
auto cmp = [](pair<int, int> a, pair<int, int> b) {
return abs(a.first - a.second) > abs(b.first - b.second);
};
set<int> s;
priority_queue<pair<int, int>, vector<pair<int, int> >, decltype(cmp)> pq(cmp);
void inserare() {
int x;
fin >> x;
if (s.find(x) == s.end()) {
s.insert(x);
auto pz = s.find(x);
if (pz != s.begin())
{
--pz;
pq.push({*pz, x});
++pz;
}
if (pz != s.end()) {
++pz;
if (pz != s.end()) {
pq.push({x, *pz});
}
}
}
}
void sterge() {
int x;
fin >> x;
if (s.find(x) != s.end()) {
auto it = s.find(x);
auto it2 = it;
++it2;
if (it != s.begin() && it2 != s.end()) {
--it;
pq.push({*it, *it2});
}
s.erase(x);
} else fout << -1 << '\n';
}
void cauta() {
int x;
fin >> x;
fout << (s.find(x) != s.end()) << '\n';
}
int maxx() {
if (s.size() < 2)
fout << -1 << '\n';
else {
auto it = s.end();
--it;
fout << *it - *s.begin()
<< '\n';
}
}
int minn() {
if (s.size() < 2)
fout << -1 << '\n';
else {
while (s.find(pq.top().first) == s.end() || s.find(pq.top().second) == s.end())
pq.pop();
fout << abs(pq.top().first - pq.top().second)
<< '\n';
}
}
int main() {
string tip;
while (fin >> tip) {
if (tip == "I") {
inserare();
} else if (tip == "S") {
sterge();
} else if (tip == "C") {
cauta();
} else if (tip == "MAX") {
maxx();
} else {
minn();
}
}
}