Cod sursa(job #2898266)

Utilizator biancar28Radulescu Alexia-Bianca biancar28 Data 6 mai 2022 15:31:50
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <iostream>
#include <fstream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

ifstream f("zeap.in");
ofstream g("zeap.out");

string st;
int x;
set<int>Z;
typedef pair<int, int> pd;

struct myComp {
    constexpr bool operator()(
            pair<int, int> const& a,
            pair<int, int> const& b)
    const noexcept
    {

        return abs(a.first-a.second) > abs(b.first-b.second);
    }
};
priority_queue<pd, vector<pd>, myComp> zeap;


void insert(int n)
{
    Z.insert(n);
    set<int>::iterator itr,p,s;
    itr=Z.find(n);
    p=Z.find(n);
    s=Z.find(n);
    s++;
    if(Z.size()>1) {
        if (itr != Z.begin()) {
            p--;
            zeap.push(make_pair(*p, *itr));
        }
        if (s != Z.end()) {
            zeap.push(make_pair(*itr, *s));
        }
    }
}

void erase(int n){

    set<int>::iterator itr,p,s;
    itr=Z.find(n);
    p=Z.find(n);
    s=Z.find(n);
    if(itr!=Z.begin()){
        p--;
    }
    if(itr++!=Z.end()){
        s++;
    }
    zeap.push(make_pair(*p, *s));
    Z.erase(n);

}

void max_dif(){
    set<int>::iterator b,e,itr;
    b=Z.begin();
    e=Z.end();
    e--;
    g<<*e-*b<<"\n";
}

void min_dif(){

    while(Z.find(zeap.top().first)==Z.end() || Z.find(zeap.top().second)==Z.end()){
        zeap.pop();
    }
    g<<abs(zeap.top().first-zeap.top().second)<<"\n";

}



int main()
{
    while(f>>st){
        if(st=="I"){
            f>>x;
            if(Z.find(x)==Z.end()){
                insert(x);
            }
        }
        if(st=="S"){
            f>>x;
            if(Z.find(x)==Z.end()){
                g<<-1<<"\n";

            }
            else{
                erase(x);
            }
        }
        if(st=="C"){
            f>>x;
            if(Z.find(x)==Z.end()){
                g<<0<<"\n";
            }
            else{
                g<<1<<"\n";
            }
        }
        if(st=="MIN"){
            if(Z.size()<2){
                g<<-1<<"\n";

            }
            else{
                min_dif();
            }
        }
        if(st=="MAX"){
            if(Z.size()<2){
                g<<-1<<"\n";

            }
            else{
                max_dif();
            }
        }
    }

    return 0;
}