Cod sursa(job #2749634)

Utilizator Andreea__Zavoiu Andreea Andreea__ Data 7 mai 2021 15:37:58
Problema Zeap Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <map>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");

int main()
{
    int x;
    char op[4];
    vector<int> arr;
    // A hash where keys are array elements and vlaues are
    // indexes in arr[]
    map <int, int> Map;

    while ( fin>>op )
    {
        if (op == "I") {
            fin>>x;
            // put element at the end of arr[]
            int index = arr.size();
            arr.push_back(x);
            // and hashmap also
            Map.insert(pair<int,int>(x, index));
        }
        else if (op == "S")
        {
            fin>>x;
            if( Map.find(x) == Map.end() ) { // elementul nu exista
                fout<<-1<<'\n';
                continue;
            }
            else {
                int index = Map.at(x);
                Map.erase(x);
                // swap with last element in arr, then 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 if (op == "C")
        {
            if(Map.find(x) != Map.end())
                fout<<1<<'\n'; // exista
            else fout<<0<<'\n'; // nu exista
        }
        else if (op == "MAX"){
            sort(arr.begin(), arr.end());
            fout<< arr[0] - arr[arr.size()-1] <<'\n';
        }
        else if (op=="MIN"){
            int dif = 1000000000;
            for (int i=0; i<arr.size()-1; i++){
                if (arr[i+1] - arr[i] < dif)
                    dif = arr[i+1]-arr[i];
            }

            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