Cod sursa(job #2751907)

Utilizator AndreeaGeamanuAndreea AndreeaGeamanu Data 16 mai 2021 02:39:10
Problema Zeap Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.48 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>

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

void insereaza(set<int> &z, int x, multiset<int> &dif){
    if(z.count(x)==0) {
    z.insert(x);
    if(z.size()>1){
    set<int>::iterator it, it1, it2, itaux;
    itaux=z.end();
    itaux--;
    int d1, d2;
    it=z.find(x);
    if(it==z.begin()){
        it1=it;
        it1++;
        d1=abs(*it-*it1);
        dif.insert(d1);
    }
    else if(it==itaux){
        it1=it;
        it1--;
        d1=abs(*it-*it1);
        dif.insert(d1);
    }
    else {
        it1=it;
        it1++;
        it2=it;
        it2--;
        d1=abs(*it-*it1);
        d2=abs(*it-*it2);
        dif.insert(d1);
        dif.insert(d2);
    }
    }
    }
}

void sterge(set<int> &z, int x, multiset<int> &dif){
    if(z.count(x)==0){
        g<<-1<<"\n";
        return;
    }
    if(z.size()>1){
    set<int>::iterator it, it1, it2, itaux;
    itaux=z.end();
    itaux--;
    int d1, d2;
    it=z.find(x);
    if(it==z.begin()){
        it1=it;
        it1++;
        d1=abs(*it-*it1);
        dif.erase(dif.find(d1));
    }
    else if(it==itaux){
        it1=it;
        it1--;
        d1=abs(*it-*it1);
        dif.erase(dif.find(d1));
    }
    else {
        it1=it;
        it1++;
        it2=it;
        it2--;
        d1=abs(*it-*it1);
        d2=abs(*it-*it2);
        dif.erase(dif.find(d1));
        dif.erase(dif.find(d2));
        int d3;
        d3=abs(*it1-*it2);
        dif.insert(d3);
    }
    }
    z.erase(x);
}

void cauta(set<int> z, int x){
    g<<z.count(x)<<"\n";

}

void maxDif(set<int> z){
    if(z.size()<2){
        g<<-1<<"\n";
        return;
    }
    g<<abs(*z.begin()-*z.rbegin())<<"\n";
}

void minDif(multiset<int> dif){
    g<<*dif.begin()<<"\n";
}

int main(){
    int x;
    char op;
    set<int> z;
    multiset<int> dif;
    while(f>>op){
        if(op=='I'){
            f>>x;
            insereaza(z,x,dif);
        }
        if(op=='S'){
            f>>x;
            sterge(z,x,dif);
        }
        if(op=='C'){
            f>>x;
            cauta(z,x);
        }
        if(op=='M'){
            char op1, op2;
            f>>op1>>op2;
            if(op1=='A'){
                maxDif(z);
            }
            if(op1=='I'){
                minDif(dif);
            }
        }
    }

    f.close();
    g.close();
    return 0;
}