Cod sursa(job #1523656)

Utilizator DiClauDan Claudiu DiClau Data 12 noiembrie 2015 22:27:43
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include<stdio.h>
using namespace std;
const int N = 200005;
int v[N], h[N], m, poz[N], ct = 0;
void schimba (int p, int x)
{
    int aux;
    aux = h[p];
    h[p] = h[x];
    h[x] = aux;
    poz[h[p]] = p;
    poz[h[x]] = x;
}
void urca (int p)
{
    while (p > 1 && v[h[p]] < v[h[p / 2]])
    {
        schimba (p, p / 2);
        p /= 2;
    }
}
void coboara (int p)
{
    int fS, fD;
    fS = p * 2;
    fD = p * 2 + 1;
    int bun = p;
    if (fS <= m && v[h[fS]] < v[h[bun]])
        bun = fS;
    if (fD <= m && v[h[fD]] < v[h[bun]])
        bun = fD;
    if (p != bun)
    {
        schimba (bun, p);
        coboara (bun);
    }
}
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, p;
    for (i = 1; i <= n; i++)
    {
        fscanf (in, "%d", &cerinta);
        if (cerinta == 1)
        {
            fscanf (in, "%d", &x);
            poz[++ct] = ++m;
            h[m] = ct;
            v[ct] = x;
            urca(m);
        }
        if (cerinta == 2)
        {
            fscanf (in, "%d", &x);
            p = poz[x];
            schimba (p, m);
            m--;
            urca(p);
            coboara(p);
        }
        if (cerinta == 3)
            fprintf (out, "%d\n", v[h[1]]);
    }
    return 0;
}