Cod sursa(job #2616523)

Utilizator adriangh3Adrian Gheorghiu adriangh3 Data 18 mai 2020 19:17:03
Problema Heapuri Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 3.13 kb
#include <stdio.h>
#include <stdlib.h>

typedef struct Heap
{
    int *h;
    int (*cmp_dist)(int *dist, int a, int b);
    int n;
    int cap;
    int *pos;
} Heap;

int cmp(int *val, int a, int b)
{
    return val[b] - val[a];
}

Heap *init_heap(int cap, int (*cmp_dist)(int *dist, int a, int b))
{
    Heap *heap = (Heap *)malloc(sizeof(Heap));
    heap->cap = cap;
    heap->n = 0;
    heap->h = (int *)malloc(cap * sizeof(int));
    heap->cmp_dist = cmp_dist;
    heap->pos = (int *)malloc(cap * sizeof(int));
    return heap;
}

void sift_down(Heap *heap, int *dist, int poz)
{
    int left = 2 * poz + 1;
    int right = 2 * poz + 2;
    int next = -1;
    if (left < heap->n)
    {
        if (heap->cmp_dist(dist, heap->h[poz], heap->h[left]) < 0)
            next = left;
        if (right < heap->n)
        {
            if (next == -1)
            {
                if (heap->cmp_dist(dist, heap->h[poz], heap->h[right]) < 0)
                    next = right;
            }
            else
            {
                if (heap->cmp_dist(dist, heap->h[left], heap->h[right]) < 0)
                    next = right;
            }
        }
    }
    if (next != -1)
    {
        int aux = heap->h[poz];
        heap->h[poz] = heap->h[next];
        heap->h[next] = aux;
        heap->pos[heap->h[poz]] = poz;
        heap->pos[heap->h[next]] = next;
        sift_down(heap, dist, next);
    }
}

void sift_up(Heap *heap, int *dist, int poz)
{
    int parent = (poz + 1) / 2 - 1;
    if (parent < 0)
        return;
    if (heap->cmp_dist(dist, heap->h[parent], heap->h[poz]) < 0)
    {
        int aux = heap->h[parent];
        heap->h[parent] = heap->h[poz];
        heap->h[poz] = aux;
        heap->pos[heap->h[poz]] = poz;
        heap->pos[heap->h[parent]] = parent;
        sift_up(heap, dist, parent);
    }
}

void insert_heap(Heap *heap, int *dist, int val)
{
    heap->h[heap->n++] = val;
    heap->pos[val] = heap->n - 1;
    sift_up(heap, dist, heap->n - 1);
}

int extract_top(Heap *heap, int *dist)
{
    int aux = heap->h[0];
    heap->h[0] = heap->h[heap->n - 1];
    heap->n--;
    sift_down(heap, dist, 0);
    return aux;
}

void free_heap(Heap *heap)
{
    free(heap->h);
    free(heap->pos);
    free(heap);
}

void delete_heap(Heap *heap, int *dist, int x)
{
    heap->h[heap->pos[x]] = heap->h[heap->n - 1];
    heap->pos[heap->h[heap->n - 1]] = heap->pos[x];
    heap->n--;
    sift_up(heap, dist, heap->pos[x]);
    sift_down(heap, dist, heap->pos[x]);
}

int main()
{
    FILE *in = fopen("heapuri.in", "r");
    FILE *out = fopen("heapuri.out", "w");
    int n, op, x, time = 0;
    fscanf(in, "%d", &n);
    int *val = (int *)malloc(n * sizeof(int));
    Heap *heap = init_heap(n, cmp);
    for (int i = 0; i < n; i++)
    {
        fscanf(in, "%d", &op);
        if (op != 3)
        {
            fscanf(in, "%d", &x);
            if (op == 1)
            {
                val[time] = x;
                insert_heap(heap, val, time);
                time++;
            }
            else
                delete_heap(heap, val, x - 1);
        }
        else
            fprintf(out, "%d\n", val[heap->h[0]]);
    }
    fclose(in);
    fclose(out);
    return 0;
}