Cod sursa(job #1522298)

Utilizator DiClauDan Claudiu DiClau Data 11 noiembrie 2015 16:04:58
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 kb
#include<stdio.h>
using namespace std;
const int N = 200005;
int v[N], m = 0, ord[N], h[N];
void adauga (int x)
{
    v[++m] = x;
    int aux, ct = m;
    while (ct / 2 != 0 && x < v[ct / 2])
    {
        aux = v[ct / 2];
        v[ct / 2] = x;
        h[v[ct / 2]] = ct / 2;
        v[ct] = aux;
        h[v[ct]] = ct;
        ct /= 2;
    }
}
void scoate (int x)
{
    v[x] = v[m];
    m--;
    int aux;
    while (x > 0 && v[x] < v[x / 2])
    {
        aux = v[x / 2];
        v[x / 2] = v[x];
        v[x] = aux;
        h[v[x / 2]] = x / 2;
        h[v[x]] = x;
        x = x / 2;
    }
    while ((2 * x <= m && v[x] > v[2 * x]) || (2  * x + 1 <= m && v[x] > v[2 * x + 1]))
    {
        if (v[2 * x] < v[2 * x + 1] && v[2 * x] < v[x])
        {
            aux = v[x];
            v[x] = v[2 * x];
            v[2 * x] = aux;
            h[v[2 * x]] = 2 * x;
            h[v[x]] = x;
            x *= 2;

        }
        else
        {
            aux = v[x];
            v[x] = v[2 * x + 1];
            v[2 * x + 1] = aux;
            h[v[x]] = x;
            h[v[2 * x + 1]] = 2 * x + 1;
            x = x * 2 + 1;
        }
    }
}
int main ()
{
    FILE *in, *out;
    in = fopen ("heapuri.in", "r");
    out = fopen ("heapuri.out", "w");
    int n;
    fscanf (in, "%d", &n);
    int i, x, cerinta, c = 0;
    for (i = 1; i <= n; i++)
    {
        fscanf (in, "%d", &cerinta);
        if (cerinta == 1)
        {
            fscanf (in, "%d", &x);
            adauga(x);
            ord[++c] = x;
        }
        if (cerinta == 2)
        {
            fscanf (in, "%d", &x);
            scoate (h[ord[x]]);
        }
        if (cerinta == 3)
            fprintf (out, "%d\n", v[1]);
    }
    return 0;
}