Cod sursa(job #2287306)

Utilizator raduandreicaRadu Andreica raduandreica Data 21 noiembrie 2018 19:20:35
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.76 kb
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
unsigned int h[2][200000],val[200000],poz[2000000],k;
int toph=1;
void push(int x)
{
    int a,b;
    k=toph++;
    h[1][k]=x; h[2][k]=k;
    while(k/2>0 && h[1][k/2]<h[1][k])
    {
        a=h[1][k/2];            b=h[2][k/2];
        h[1][k/2]=h[1][k];      h[2][k/2]=h[2][k];
        h[1][k]=a;              h[2][k]=b;

        k=k/2;
    }
}

void pop(int x)
{
    int a,b; b=h[2][x];
    while (h[2][b]!=x) b=h[2][h[2][b]];
    h[1][b]=h[1][toph-1];
    h[2][b]=h[2][toph-1];
    toph--; h[1][toph]=NULL;h[2][toph]=NULL;
    k=b;
    if(h[1][k]<h[1][k*2]||h[1][k]<h[1][k*2+1]) while(h[1][k*2]||(h[1][k]>h[1][k*2]&&h[1][k]>h[1][k*2+1]))
    {
        if(h[1][k]<h[1][k*2]&&h[1][k*2]>h[1][k*2+1]) {a=h[1][k];h[1][k]=h[1][k*2];h[1][k*2]=a;
                                                      a=h[2][k];h[2][k]=h[2][k*2];h[2][k*2]=a;}
        if(h[1][k]<h[1][k*2+1]&&h[1][k*2+1]>h[1][k*2]) {a=h[1][k];h[1][k]=h[1][k*2+1];h[1][k*2+1]=a;
                                                        a=h[2][k];h[2][k]=h[2][k*2+1];h[2][k*2+1]=a;}
    }
    if(h[1][k]>h[1][k/2])while(k/2>0 && h[1][k/2]<h[1][k])
    {
        a=h[1][k/2];            b=h[2][k/2];
        h[1][k/2]=h[1][k];      h[2][k/2]=h[2][k];
        h[1][k]=a;              h[2][k]=b;

        k=k/2;
    }
}
int main()
{
    ifstream fin("heapuri.in"); ofstream fout("heapuri.out");
    int N,x,y,i,z,c;
    fin>>N;
    for(i=1;i<=N;i++)
    {
        fin>>x;
        if(x==1) {fin>>y; push(y);}
        if(x==2) {fin>>y; pop(y);}
        if(x==3)
        {
            c=toph/2;z=1000000001;
            while(h[1][c]) {if(h[1][c]<z) z=h[1][c];c++;}
            fout<<z;
        }
    }
}