Cod sursa(job #3237630)

Utilizator CondoracheAlexandruCondorache Alexandru CondoracheAlexandru Data 10 iulie 2024 20:41:28
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.47 kb
#include <bits/stdc++.h>
#include <set>

const int maxn = 2e5 + 5;

typedef struct heap {
    int capacity;
    int curr_size;
    int *arr;
    int (*func)(int, int);
}heap_t;

int max_heap(int a, int b);
int min_heap(int a, int b);

heap_t* init(int n, int (*cmp)(int, int));
void insert(heap_t* heap, int x);
void insert_help(heap_t* heap, int idx, int (*fcmp)(int, int));
int extract(heap_t* heap);
void heapify(heap_t* heap, int idx, int (*fcmp)(int, int));

int max_heap(int a, int b) {
    return a > b;
}

int min_heap(int a, int b) {
    return a < b;
}

heap_t* init(int cap, int (*cmp)(int, int)) {
    heap_t* heap = (heap_t *)calloc(1, sizeof(heap_t));
    if (!heap) {
        return NULL;
    }
    heap->capacity = cap;
    heap->arr = (int *)calloc(cap, sizeof(int));
    if (!heap->arr) {
        free(heap);
        return NULL;
    }
    heap->func = cmp;
    return heap;
}

void insert(heap_t* heap, int x) {
    if (heap->curr_size >= heap->capacity) {
        printf("loh\n");
        return;
    }
    heap->arr[heap->curr_size] = x;
    heap->curr_size++;
    insert_help(heap, heap->curr_size - 1, heap->func);
}

void insert_help(heap_t* heap, int idx, int (*fcmp)(int, int)) {
    int parent = (idx - 1) / 2;
    if (fcmp(heap->arr[idx], heap->arr[parent])) {
        int temp = heap->arr[parent];
        heap->arr[parent] = heap->arr[idx];
        heap->arr[idx] = temp;
        insert_help(heap, parent, heap->func);
    }
}

int extract(heap_t* heap) {
    if (!heap->curr_size || !heap) {
        printf("Heap is empty!\n");
        return INT_MIN;
    }
    int extr = heap->arr[0];
    heap->arr[0] = heap->arr[heap->curr_size - 1];
    heap->curr_size--;
    heapify(heap, 0, heap->func);
    return extr;
}

void heapify(heap_t* heap, int idx, int (*fcmp)(int, int)) {
    int left = 2 * idx + 1;
    int right = 2 * idx + 2;
    int child = idx;
    if (left >= heap->capacity) {
        left = -1;
    }
    if (right >= heap->capacity) {
        right = -1;
    }
    if (left != -1 && fcmp(heap->arr[left], heap->arr[child])) {
        child = left;
    }
    if (right != -1 && fcmp(heap->arr[right], heap->arr[child])) {
        child = right;
    }
    if (child != idx) {
        int temp = heap->arr[child];
        heap->arr[child] = heap->arr[idx];
        heap->arr[idx] = temp;
        heapify(heap, child, heap->func);
    }
}

int main() {
    FILE* input = fopen("heapuri.in", "r");
    FILE* output = fopen("heapuri.out", "w");
    int n = 0;
    fscanf(input, "%d", &n);
    std::vector<int> a;
    heap_t* heap = init(maxn, min_heap);
    for (int i = 0; i < n; i++) {
        int type = 0;
        fscanf(input, "%d", &type);
        if (type == 1) {
            int x = 0;
            a.push_back(x);
            fscanf(input, "%d", &x);
            insert(heap, x);
        }
        else if (type == 2) {
            int x = 0;
            fscanf(input, "%d", &x);
            int idx = 0;
            for (int i = 0; i< a.size(); i++) {
                if (a[i] == x) {
                    idx = i;
                    break;
                }
            }
            int temp = heap->arr[idx];
            heap->arr[idx] = heap->arr[heap->curr_size - 1];
            heap->arr[heap->curr_size - 1] = temp;
            heapify(heap, idx, min_heap);
        }
        else {
            fprintf(output, "%d\n", heap->arr[0]);
        }
    }
    return 0;
}