Cod sursa(job #2899071)

Utilizator Petrica81Simion Petrica Petrica81 Data 7 mai 2022 17:48:46
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.13 kb
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
ifstream f ("zeap.in");
ofstream g ("zeap.out");
unordered_map<int, int> arbore;
string aux;
vector<int> heap;
int x,maxim = -1;

void inserare(int x){
    if(!arbore[x]){
        heap.push_back(x);
        int i = heap.size()-1;
        arbore[x] = i;
        while(heap[i/2] > heap[i]){
            arbore[x] = i/2;
            arbore[heap[i/2]] = i;
            swap(heap[i/2],heap[i]);
            i /= 2;
        }
    }
    if(maxim < x) maxim = x;
}
void stergere(int x){
    if(!arbore[x]){
        g<<"-1\n";
    }
    else{
        if(x == maxim) maxim = -1;
        int i = heap.size()-1, x2 = heap[i];
        arbore[heap[i]] =  arbore[x];
        heap[arbore[x]] = heap[i];
        arbore[x] = 0;
        heap.pop_back();
        x = x2;
        i = arbore[x];
        while(heap[i/2] > heap[i]){
            arbore[x] = i/2;
            arbore[heap[i/2]] = i;
            swap(heap[i/2],heap[i]);
            i /= 2;
        }
        bool ok = false;
        while(!ok){
            if(i*2+1 < heap.size()){
                if(heap[i] > heap[i*2+1] || heap[i] > heap[i*2]){
                    if(heap[i*2] > heap[i*2+1]){
                        swap(arbore[heap[i*2+1]],arbore[heap[i]]);
                        swap( heap[i*2+1], heap[i]);
                        i = i*2+1;
                    }
                    else {
                        swap(arbore[heap[i*2]],arbore[heap[i]]);
                        swap( heap[i*2], heap[i]);
                        i *= 2;
                    }
                }
                else ok = true;
            }
            else if(i*2 < heap.size()){
                if(heap[i*2] < heap[i]){
                    swap(arbore[heap[i*2]],arbore[heap[i]]);
                    swap( heap[i*2], heap[i]);
                    i *= 2;
                }
                ok = true;
            }
            else ok = true;
        }

    }
}
void MAX(){
    x =  heap.size();
    if(x >= 3){
        if(maxim != -1){
            g<<maxim - heap[1]<<'\n';
        }
        else{
            for(int i = x-1; i >= (x-1)/2 ;i--){
                if(heap[i]> maxim) maxim = heap[i];
            }
            g<<maxim - heap[1]<<'\n';
        }
    }
    else g<<"-1\n";
}
void MIN(){
    x =  heap.size();
    if(x >= 3){
        int mindif = heap[2]- heap[1];
        for(int i = 1; i < x-1;i++)
            for(int j = i+1; j < i*2+1 && j < x; j++){
                int y = abs(heap[i]-heap[j]);
                if(y < mindif) mindif = y;
            }
        g<<mindif<<'\n';
    }
    else g<<"-1\n";
}
int main() {
    heap.push_back(0);
    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[x] != 0 ) g<<"1\n";
            else g<<"0\n";
        }
        else if(aux == "I"){
            f>>x;
            inserare(x);
        }
    }
//    for(int i = 1; i < heap.size(); i++)
//        cout<<heap[i]<<" ";

    return 0;
}