Cod sursa(job #3252658)

Utilizator ArthurrrfubinacaARthur Paun Arthurrrfubinaca Data 30 octombrie 2024 15:36:04
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.37 kb
#include <iostream>
#include <fstream>
using namespace std;

const int MAX_SIZE = 200000;

int heap[MAX_SIZE + 1];           // Min-heap array
int elements[MAX_SIZE + 1];        // Array to store elements in insertion order
int position[MAX_SIZE + 1];        // Array to keep track of positions in the heap
int heapSize = 0;                  // Size of the min-heap
int elementCount = 0;              // Total count of elements inserted

// Helper function to swap two elements in the heap
void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

// Function to insert an element into the min-heap
void insertHeap(int index) {
    heap[++heapSize] = index; // Insert the index of the new element
    position[index] = heapSize; // Update the position array
    int i = heapSize;
    while (i > 1 && elements[heap[i]] < elements[heap[i / 2]]) {
        swap(heap[i], heap[i / 2]);
        position[heap[i]] = i; // Update the position after swapping
        position[heap[i / 2]] = i / 2; // Update the position after swapping
        i /= 2;
    }
}

// Function to delete an element from the heap by index
void deleteFromHeap(int index) {
    int pos = position[index]; // Get the position of the element to delete
    if (pos == 0) return; // If the position is invalid, exit

    // Swap the element with the last element in the heap
    swap(heap[pos], heap[heapSize--]);
    position[heap[pos]] = pos; // Update position of the new root
    position[index] = 0; // Mark the deleted element as invalid

    // Restore heap property by heapifying down
    int i = pos;
    while (true) {
        int smallest = i;
        // Check left child
        if (2 * i <= heapSize && elements[heap[2 * i]] < elements[heap[smallest]]) {
            smallest = 2 * i;
        }
        // Check right child
        if (2 * i + 1 <= heapSize && elements[heap[2 * i + 1]] < elements[heap[smallest]]) {
            smallest = 2 * i + 1;
        }
        // If the smallest is not the current node, swap
        if (smallest != i) {
            swap(heap[i], heap[smallest]);
            position[heap[i]] = i; // Update the positions
            position[heap[smallest]] = smallest; // Update the positions
            i = smallest; // Move down to the next level
        } else {
            break; // Heap property restored
        }
    }
}

// Function to get the minimum element from the heap
int getMin() {
    return elements[heap[1]];  // Return the true minimum
}

int main() {
    ifstream fin("heapuri.in");
    ofstream fout("heapuri.out");

    int N;
    fin >> N;

    for (int i = 1; i <= N; ++i) {  // Change index to start from 1 for elements
        int operation, x;
        fin >> operation;

        if (operation == 1) {
            // Insert operation
            fin >> x;
            elements[++elementCount] = x;  // Store element in chronological order
            insertHeap(elementCount);      // Insert the index of the element into the heap

        } else if (operation == 2) {
            // Delete operation
            fin >> x;
            deleteFromHeap(x);  // Remove the x-th inserted element

        } else if (operation == 3) {
            // Minimum query
            fout << getMin() << '\n';  // Output the minimum element
        }
    }

    fin.close();
    fout.close();
    return 0;
}