Cod sursa(job #2899281)

Utilizator Petrica81Simion Petrica Petrica81 Data 8 mai 2022 13:53:58
Problema Zeap Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <iostream>
#include <fstream>
#include <set>
#include <string>
#include <queue>
#include <tuple>
using namespace std;
ifstream f ("zeap.in");
ofstream g ("zeap.out");
set<int> arbore;
string aux;
priority_queue<tuple<int,int,int>, vector<tuple<int,int,int>>, greater<tuple<int,int,int>>> dif;
int x,maxim = -1;

void inserare(int x){
    if(arbore.find(x) == arbore.end()){
        arbore.insert(x);
        auto it = arbore.find(x);
        auto st = it;
        auto dr = it;
        dr++;
        st--;
        if(dr != arbore.end()){
            dif.push(make_tuple(*dr - *it, *it, *dr));
        }
        if(it != arbore.begin()){
            dif.push(make_tuple(*it - *st, *st, *it));
        }
    }
}
void stergere(int x){
    if(arbore.find(x) == arbore.end()){
        g<<"-1\n";
    }
    else{
        auto it = arbore.find(x);
        auto st = it;
        auto dr = it;
        dr++;
        st--;
        if(it != arbore.begin() && dr != arbore.end()){
            dif.push(make_tuple(*dr - *st, *st, *dr));
        }
        arbore.erase(it);
    }
}
void MAX() {
    if (arbore.size() < 2) {
        g << "-1\n";
    }
    else {
        auto final = --arbore.end();
        g << *final - *arbore.begin() << '\n';
    }
}
void MIN() {
    if (arbore.size() >= 2) {
        while((arbore.find(get<2>(dif.top())) == arbore.end()) || (arbore.find(get<1>(dif.top())) == arbore.end())) {
            dif.pop();
        }
        g << get<0>(dif.top()) << '\n';
    }
    else {
        g << "-1\n";
    }
}
int main() {
    while(!f.eof()){
        f>>aux;
        if(aux == "MAX")MAX();
        else if(aux == "MIN")MIN();
        else if(aux == "S"){
            f>>x;
            stergere(x);
        }
        else if(aux == "C"){
            f>>x;
            if(arbore.find(x) != arbore.end() ) g<<"1\n";
            else g<<"0\n";
        }
        else if(aux == "I"){
            f>>x;
            inserare(x);
        }
    }
    while(!dif.empty()){
        cout<<get<0>(dif.top())<<" "<<get<2>(dif.top())<<" "<<get<1>(dif.top())<<" ";
        dif.pop();
        cout<<endl;
    }
    return 0;
}