Cod sursa(job #2899253)

Utilizator Petrica81Simion Petrica Petrica81 Data 8 mai 2022 13:02:03
Problema Zeap Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 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), st = --it, dr = ++it;
        if(dr != arbore.end() && st != arbore.begin()){
            dif.push(make_tuple(*it - *st, *st, *it));
            dif.push(make_tuple(*dr - *it, *it, *dr));
        }
    }
}
void stergere(int x){
    if(arbore.find(x) == arbore.end()){
        g<<"-1\n";
    }
    else{
        auto it = arbore.find(x), st = --it, dr = ++it;
        if(dr != arbore.end() && st != arbore.begin()){
            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){
    g<<"-1\n";
}
else{
    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';
}
}
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);
        }
    }
    return 0;
}