Cod sursa(job #1085403)

Utilizator LizzardStanbeca Theodor-Ionut Lizzard Data 16 ianuarie 2014 23:24:29
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.94 kb
#include <fstream>
#define _N 200001
using namespace std;

ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");

struct entry
{
    int val;
    int pos;
};

void ins(entry [] , int , int , int );
void del(entry [] , int );

int main()
{
    bool exp[_N]= {0};
    entry heap[_N];
    int n;
    int v1,v2;

    fin>>n;
    for(int i=1,ne=0,p=1; i<=n; i++)
    {
        fin>>v1;
        switch(v1)
        {
        case 1:
            fin>>v2;
            ins(heap,v2,ne,p);
            p++;
            ne++;
            break;
        case 2:
            fin>>v2;
            exp[v2]=true;
            break;
        case 3:
            while(exp[heap[1].pos]==true)
            {
                del(heap,ne);
                ne--;
            }
            fout<<heap[1].val<<"\n";
            break;
        }
    }

    return 0;
}

void del(entry heap[], int n)
{
    int p1,p2,p=1;
    entry aux;
    heap[1]=heap[n];
    n--;

    p1=2*p;
    p2=2*p+1;
    while((heap[p].val>heap[p1].val || heap[p].val>heap[p2].val)&& p1<=n && p2<=n)
    {
        if(heap[p].val>heap[p1].val && heap[p1].val<heap[p2].val)
        {
            aux=heap[p];
            heap[p]=heap[p1];
            heap[p1]=aux;

            p=p1;
            p1=p*2;
            p2=p*2+1;
            continue;
        }
        if(heap[p].val>heap[p2].val && heap[p2].val<heap[p1].val)
        {
            aux=heap[p];
            heap[p]=heap[p2];
            heap[p2]=aux;

            p=p2;
            p1=p*2;
            p2=p*2+1;
            continue;
        }
    }
    if(p1<=n)
        if(heap[p].val>heap[p1].val)
        {
            aux=heap[p];
            heap[p]=heap[p1];
            heap[p1]=aux;
        }
}

void ins(entry heap[], int x, int n, int i)
{
    entry aux;
    int p=++n;
    heap[n].val=x;
    heap[n].pos=i;

    while(heap[p].val<heap[p/2].val && p/2>0)
    {
        aux=heap[p];
        heap[p]=heap[p/2];
        heap[p/2]=aux;

        p/=2;
    }
}