Cod sursa(job #2749747)

Utilizator Andreea__Zavoiu Andreea Andreea__ Data 8 mai 2021 09:30:07
Problema Zeap Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.94 kb
/* Cerinta: un zeap mentine o multime de numere naturale*/ ///distincte
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <map>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");

int main()
{
    int dif = 1000000000;
    int x;
    char op[4];
    vector<int> arr;
    // A hash where keys are array elements and values are indexes in arr[] -> Maps are typically implemented as binary search trees.
    map <int, int> Map; // le stocheaza sortate in ordine crescatoare!

    while ( fin>>op )
    {
        if (op == "I") {
            fin>>x;
            // put element at the end of arr[]
            int index = arr.size();
            arr.push_back(x); // a inserat pe pozitia index pt ca numerotarea se face de la 0
            // and hashmap also
            Map.insert(pair<int,int>(x, index)); // cheia e x, valoarea e index
        }
        else if (op == "S")
        {
            fin>>x;
            if( Map.find(x) != Map.end() ) {
                int index = Map.at(x); // valoarea (indexul) corespunzatoare cheii x
                Map.erase(x);
                // swap this with the last element in arr to remove element at back
                int last = arr.size() - 1;
                swap(arr[index], arr[last]);
                arr.pop_back();
                // Update hash table for new index of last element
                Map.at(arr[index]) = index;
            }
            else fout<<-1<<'\n'; // elementul nu exista
        }
        else if (op == "C")
        {
            fin>>x;
            fout << (Map.find(x) != Map.end()) <<'\n'; // 0 sau 1
        }
        else if (op == "MAX"){
            if(Map.size() < 2)
                fout << -1 << '\n';
            else {
                auto it = Map.end(); // iterator
                --it; // din nou din cauza indexarii de la 0
                fout << *it - *Map.begin() << '\n'; // diferenta valorilor de pe prima si ultima pozitie (cel
            }
        }
        else if (op=="MIN"){
        /*  if(Map.size() < 2)
                fout << -1 << '\n';
            else{
                while( Map.find(pq.top().first) == s.end() || s.find(pq.top().second) == s.end())
                    pq.pop();
                fout << abs(pq.top().first - pq.top().second) << '\n';
            }*/
            auto i = Map.begin();
           while ( i!= Map.end() ) {
                auto primul = i->first;
                ++i;
                if (abs( primul - i->first) < dif)
                    dif = abs( primul - i->first);
           }
           fout<<dif<<'\n';
        }
    }

    return 0;
}
// https://www.geeksforgeeks.org/design-a-data-structure-that-supports-insert-delete-search-and-getrandom-in-constant-time/?ref=rp
// https://www.pbinfo.ro/articole/4552/set-map
// https://www.pbinfo.ro/articole/12533/cpp-stl-vector
//https://www.pbinfo.ro/articole/10743/stl-sort