Cod sursa(job #1453770)

Utilizator alex.bullzAlexandru Lilian alex.bullz Data 24 iunie 2015 18:00:11
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.77 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#define N 200010

class Heap {
private:
    std::vector<int> nodes;
    int pos[N];
public:
    Heap() {
        nodes.push_back (-1);
        memset (pos, -1, N);
    }

    int size() {
        return nodes.size();
    }

    int getParent (int index) {
        if (index > 1) {
            return index / 2;
        } 

        return -1;
    }

    int getChild1 (int index) {
        if (2 * index <= nodes.size()) {
            return 2 * index;
        }

        return -1;
    }

    int getChild2 (int index) {
        if (2 * index + 1 <= nodes.size()) {
            return 2 * index + 1;
        }

        return -1;
    }

    int getMin() {
        return nodes[1];
    }

    void moveUp (int element) {
        if (nodes[element] < nodes[getParent (element)] &&
                getParent (element) > 0) {

            int aux = nodes[element];
            nodes[element] = nodes[getParent (element)];
            nodes[getParent (element)] = aux;

            pos[nodes[element]] = getParent (element);
            pos[nodes[getParent (element)]] = element;

            moveUp (getParent (element));
        } else {
            return;
        }
    }

    void moveDown (int index) {
        if (    (nodes[index] > nodes[getChild1 (index)] ||
                nodes[index] > nodes[getChild1 (index)]) &&
                getChild1 (index) < size() && getChild2 (index) < size()) {

            int max = -1;
            if (nodes[getChild1 (index)] > nodes[getChild2 (index)]) {
                max = getChild1 (index);
            } else {
                max = getChild2 (index);
            }

            int aux = nodes[index];
            nodes[index] = nodes[max];
            nodes[max] = aux;

            pos[nodes[index]] = max;
            pos[nodes[max]] = index;

            moveDown (max);
        } else {
            return;
        }
    }

    void insert (int element) {
        nodes.push_back (element);
        moveUp (size() - 1);
    }

    void remove (int element) {
        nodes[pos[element]] = nodes[size()-1];
        nodes.pop_back();
        moveDown (pos[element]);
        moveUp (pos[element]);
    }
};

int main() {
    int numberOfOperations, operation, element;
    Heap *heap = new Heap();
    std::ifstream fin ("heapuri.in");
    std::ofstream fout ("heapuri.out");

    fin >> numberOfOperations;
    
    for (int i = 0; i < numberOfOperations; ++i) {
        fin >> operation;

        if (operation == 3) {
            int min = heap->getMin();
            fout << min << " ";
            continue;
        }

        fin >> element;

        if (operation == 1) {
            heap->insert (element);
        }

        if (operation == 2) {
            heap->remove (element);
        }
    }
}