Cod sursa(job #1082961)

Utilizator tavi.belu1994FMI Belu Andrei Octavian tavi.belu1994 Data 15 ianuarie 2014 14:30:36
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#define nmax 200001
FILE *f,*g;
using namespace std;

int pozitie,POZ[nmax],POZ2[nmax],k,k2;
int n;

void urca(int *heap, int &poz)
{
    while(poz >= 1 && heap[poz] < heap[poz/2])
    {
        swap(heap[poz] , heap[poz/2]);
        pozitie = poz;

        poz /= 2;
    }
}

void coboara(int *heap, int &poz)
{
    while(poz * 2 <= n && heap[poz] > heap[poz*2])
    {
        swap(heap[poz] , heap[poz*2]);
        poz *= 2;
    }
}

void insert_heap(int *heap, int val)
{
    heap[++n] = val;
    pozitie = n;
    urca(heap,n);
}

void elimina(int *heap, int poz)
{
    heap[poz] = heap[n--];
    coboara(heap,poz);
    urca(heap,poz);
}

int main()
{
    f=fopen("heapuri.in","r");
    g=fopen("heapuri.out","w");
    int i,j,N,heap[nmax],x,c;
    n = 0;
    fscanf(f,"%d",&N);
    for(i=1 ; i<=N ; i++)
    {
        fscanf(f,"%d%d",&c,&x);
        if(c == 1)
        {
            insert_heap(heap,x);
            POZ[++k] = pozitie;
            continue;
        }
        if(c == 2)
        {
            elimina(heap,POZ[x]);
            continue;
        }
        fprintf(g,"%d\n",heap[1]);
    }
    fclose(f);
    fclose(g);
    return 0;
}