Pagini recente » Statistici Victor Barabas (fericitu) | Cod sursa (job #1920554) | Cod sursa (job #791440) | Cod sursa (job #513876) | Cod sursa (job #2751619)
#include <bits/stdc++.h>
using namespace std;
FILE *input;
ofstream fout("zeap.out");
class cmp {
public:
bool operator()(pair<int, int> &X, pair<int, int> &Y) {
return abs(X.second - X.first) > abs(Y.second - Y.first);
}
};
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> dif;
set<int> S;
bool findNum(int x) {
return S.find(x) != S.end();
}
void insertNum(int x) {
if (!findNum(x)) {
S.insert(x);
}
for (auto v : S) {
if (v != x) {
dif.push(make_pair(x, v));
}
}
}
int deleteNum(int x) {
if (!findNum(x)) {
return -1;
}
S.erase(x);
return 1;
}
int maxDif() {
if (S.size() < 2) {
return -1;
}
return *S.rbegin() - *S.begin();
}
int minDif() {
pair<int, int> p = dif.top();
while (!findNum(p.first) || !findNum(p.second)) {
dif.pop();
if (dif.empty()) {
return -1;
}
p = dif.top();
}
return abs(p.second - p.first);
}
int strToNum(char *command) {
int num = 0;
for (int i = 2; command[i] != '\n'; ++i) {
num = num * 10 + command[i] - '0';
}
return num;
}
int main() {
input = fopen("zeap.in", "r");
char command[13];
while (fgets(command, 13, input)) {
if (command[0] == 'I') {
insertNum(strToNum(command));
} else if (command[0] == 'C') {
fout << findNum(strToNum(command)) << '\n';
} else if (command[0] == 'S') {
int res = deleteNum(strToNum(command));
if (res == -1) {
fout << res << '\n';
}
} else if (!strcmp(command, "MAX\n")) {
fout << maxDif() << '\n';
} else {
fout << minDif() << '\n';
}
}
fout.close();
return 0;
}