Cod sursa(job #3231180)

Utilizator catalinaionela77Catalina Ionela Florescu catalinaionela77 Data 25 mai 2024 12:18:46
Problema Heapuri Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 3.85 kb
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// Structura pentru heap
typedef struct {
    int* data;
    int* position;
    int* order;
    int size;
    int capacity;
} MinHeap;

// Functie pentru creare heap
MinHeap* createMinHeap(int capacity) {
    MinHeap* heap = (MinHeap*)malloc(sizeof(MinHeap));
    heap->data = (int*)malloc(capacity * sizeof(int));
    heap->position = (int*)malloc(capacity * sizeof(int));
    heap->order = (int*)malloc(capacity * sizeof(int));
    heap->size = 0;
    heap->capacity = capacity;
    return heap;
}

// Functie pentru eliberare memorie heap
void freeMinHeap(MinHeap* heap) {
    free(heap->data);
    free(heap->position);
    free(heap->order);
    free(heap);
}

// Functie pentru schimbare elemente in heap
void swap(MinHeap* heap, int i, int j) {
    int temp = heap->data[i];
    heap->data[i] = heap->data[j];
    heap->data[j] = temp;
    temp = heap->order[i];
    heap->order[i] = heap->order[j];
    heap->order[j] = temp;
    heap->position[heap->order[i]] = i;
    heap->position[heap->order[j]] = j;
}

// Functie heapify-up
void heapifyUp(MinHeap* heap, int i) {
    while (i > 0 && heap->data[i] < heap->data[(i - 1) / 2]) {
        swap(heap, i, (i - 1) / 2);
        i = (i - 1) / 2;
    }
}

// Functie heapify-down
void heapifyDown(MinHeap* heap, int i) {
    int left, right, smallest;
    while (1) {
        left = 2 * i + 1;
        right = 2 * i + 2;
        smallest = i;
        if (left < heap->size && heap->data[left] < heap->data[smallest]) {
            smallest = left;
        }
        if (right < heap->size && heap->data[right] < heap->data[smallest]) {
            smallest = right;
        }
        if (smallest != i) {
            swap(heap, i, smallest);
            i = smallest;
        } else {
            break;
        }
    }
}

// Functie pentru inserare in heap
void insertHeap(MinHeap* heap, int value, int pos) {
    if (heap->size == heap->capacity) {
        heap->capacity *= 2;
        heap->data = (int*)realloc(heap->data, heap->capacity * sizeof(int));
        heap->position = (int*)realloc(heap->position, heap->capacity * sizeof(int));
        heap->order = (int*)realloc(heap->order, heap->capacity * sizeof(int));
    }
    heap->data[heap->size] = value;
    heap->position[pos] = heap->size;
    heap->order[heap->size] = pos;
    heap->size++;
    heapifyUp(heap, heap->size - 1);
}

// Functie pentru extragere minim din heap
int extractMinHeap(MinHeap* heap) {
    if (heap->size == 0) {
        return INT_MAX;
    }
    int min = heap->data[0];
    swap(heap, 0, heap->size - 1);
    heap->size--;
    heapifyDown(heap, 0);
    return min;
}

// Functie pentru stergere din heap
void deleteHeap(MinHeap* heap, int pos) {
    int index = heap->position[pos];
    swap(heap, index, heap->size - 1);
    heap->size--;
    heapifyDown(heap, index);
    heapifyUp(heap, index);
}

// Functie pentru returnare minim
int getMinHeap(MinHeap* heap) {
    if (heap->size == 0) {
        return INT_MAX;
    }
    return heap->data[0];
}

// Functie principala
int main() {
    FILE *fin = fopen("heapuri.in", "r");
    FILE *fout = fopen("heapuri.out", "w");

    int N;
    fscanf(fin, "%d", &N);

    MinHeap* heap = createMinHeap(N);
    int currentIndex = 0;

    for (int i = 0; i < N; i++) {
        int cod, x;
        fscanf(fin, "%d", &cod);

        if (cod == 1) {
            fscanf(fin, "%d", &x);
            insertHeap(heap, x, currentIndex++);
        } else if (cod == 2) {
            fscanf(fin, "%d", &x);
            deleteHeap(heap, x - 1);
        } else if (cod == 3) {
            int min = getMinHeap(heap);
            fprintf(fout, "%d\n", min);
        }
    }

    freeMinHeap(heap);
    fclose(fin);
    fclose(fout);
    return 0;
}