Cod sursa(job #2899774)

Utilizator DariaClemClem Daria DariaClem Data 9 mai 2022 01:06:14
Problema Zeap Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.69 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("zeap.in");
ofstream fout("zeap.out");

set<int> valori;
priority_queue<int, vector<int>, greater<>> maxi;
priority_queue<int> mini;


int main() {
    string instructiune;
    int nr, index, min1, min2, maxi1, maxi2;
    while (fin >> instructiune) {
        if (instructiune[0] == 'M') {
            min1 = min2 = maxi1 = maxi2 = -1;
            while (valori.empty() == 0 and !(valori.find(mini.top()) != valori.end()))
                mini.pop();
            if (valori.empty() == 0)
                min1 = mini.top();
            while (valori.empty() == 0 and (!(valori.find(mini.top()) != valori.end()) or mini.top() == min1))
                mini.pop();
            if (valori.empty() == 0 and mini.top() != min1)
                min2 = mini.top();
            while (valori.empty() == 0 and !(valori.find(maxi.top()) != valori.end()))
                maxi.pop();
            if (valori.empty() == 0)
                maxi1 = maxi.top();
            while (valori.empty() == 0 and (!(valori.find(maxi.top()) != valori.end()) or maxi.top() != maxi1))
                maxi.pop();
            if (valori.empty() == 0 and maxi.top() != maxi1)
                maxi2 = maxi.top();
            if (instructiune == "MAX") {
                if (min1 > -1 and maxi1 > -1)
                    cout << abs((maxi1 > maxi2 and maxi2 != -1 ? maxi1 : maxi2) -
                                (min1 > min2 and min2 != -1 ? min1 : min2)) << "\n";
                else cout << "-1\n";
            } else {
                if ((min1 > -1 and min2 > -1) or (maxi1 > -1 and
                                                  maxi2 > -1) or (min1 > -1 and maxi2 > -1)) {
                    cout << (abs(min1 - min2) <
                             (abs(maxi1 - maxi2) < abs(maxi1 - min2) ? abs(maxi1 - maxi2) : abs(maxi1 - min2)) ? abs(
                            min1 - min2) : (abs(maxi1 - maxi2) < abs(maxi1 - min2) ? abs(maxi1 - maxi2) : abs(
                            maxi1 - min2))) << "\n";
                } else cout << "-1\n";
            }
        } else {
            fin >> nr;
            if (instructiune[0] == 'I') {
                if (valori.find(nr) == valori.end()) {
                    mini.push(nr);
                    maxi.push(nr);
                }
                valori.insert(nr);
            } else if (instructiune[0] == 'C') {
                cout << ((valori.find(nr) != valori.end()) ? "1\n" : "0\n");
            } else if (instructiune[0] == 'S') {
                if (valori.find(nr) != valori.end()) {
                    valori.erase(nr);
                } else {
                    cout << "-1\n";
                }
            }
        }
    }
    return 0;
}