Pagini recente » Cod sursa (job #1978) | Cod sursa (job #366179) | Cod sursa (job #2744473) | Cod sursa (job #651405) | Cod sursa (job #3139521)
#include <iostream>
#include <string.h>
#include <fstream>
#include <set>
using namespace std;
ifstream in("Zeap.in");
ofstream out("Zeap.out");
class Zeap
{
set<int> multime;
int min;
int min2;
int max;
public:
Zeap()
{
this->multime = {};
this->min = -1;
this->min2 = -1;
this->max = -1;
}
void insert(int x)
{
if (this->max == -1)
this->max = x;
else if (this->max < x)
this->max = x;
if (this->min == -1)
this->min = x;
else if (this->min > x)
this->min = x;
this->multime.insert(x);
}
int remove(int x)
{
auto found = this->multime.find(x);
if (found != this->multime.end())
{
this->multime.erase(x);
return 0;
}
else
return -1;
}
bool search(int x)
{
auto found = this->multime.find(x);
if (found != this->multime.end())
return 1;
else
return 0;
}
int max_dif()
{
return this->max - this->min;
}
int min_dif()
{
int temp_min = 99999;
for (int i = 0; i < sizeof(this->multime); i++)
{
auto it = this->multime.begin();
advance(it, i);
if (this->min < *it && *it < temp_min)
temp_min = *it;
}
this->min2 = temp_min;
return this->min2 - this->min;
}
};
int main()
{
Zeap zeap;
string cmd;
int x;
while (in >> cmd)
{
if (cmd == "I")
{
in >> x;
zeap.insert(x);
}
if (cmd == "S")
{
in >> x;
if (zeap.remove(x) == -1)
out << -1 << endl;
}
if (cmd == "C")
{
in >> x;
out << zeap.search(x) << endl;
}
if (cmd == "MIN")
out << zeap.min_dif() << endl;
if (cmd == "MAX")
out << zeap.max_dif() << endl;
}
in.close();
out.close();
}