Cod sursa(job #1815533)

Utilizator Ruxandra985Nanu Ruxandra Laura Ruxandra985 Data 25 noiembrie 2016 13:05:22
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.15 kb
#include <cstdio>
#include <algorithm>
#include <iostream>

using namespace std;
int h[200001],hh[200001];
int ind[200001],inc=1;
void adauga (int x,int elem){
    int p,c,aux;
    h[elem]=x;
    hh[elem]=elem;
    ind[elem]=elem;
    c=elem;
    p=elem/2;
    while (p>=0){
        if (h[c]<h[p]){
            aux=h[c];
            h[c]=h[p];
            h[p]=aux;
            aux=hh[p];
            hh[p]=hh[c];
            hh[c]=aux;
            ind[hh[p]]=p;
            ind[hh[c]]=c;
        }
        else break;
        c=p;
        p/=2;
    }
}
void sterge (int poz,int elem){
    int p,c,aux;
    c=poz;
    p=poz/2;
    h[poz]=h[elem];
    hh[poz]=hh[elem];
    ind[hh[poz]]=ind[hh[elem]];
    if (h[c]<h[p]){
        while (h[c]<h[p] && p>=0){
            aux=h[c];
            h[c]=h[p];
            h[p]=aux;
            aux=hh[p];
            hh[p]=hh[c];
            hh[c]=aux;
            ind[hh[p]]=p;
            ind[hh[c]]=c;
            c=p;
            p/=2;
        }
    }
    else {
        p=poz;
        c=2*poz;
        while (c<=elem){
            if (c+1<=elem && h[c+1]<h[c])
                c++;
            aux=h[c];
            h[c]=h[p];
            h[p]=aux;
            aux=hh[p];
            hh[p]=hh[c];
            hh[c]=aux;
            ind[hh[p]]=p;
            ind[hh[c]]=c;
            p=c;
            c*=2;
        }
    }
}
int main()
{
    FILE *fin=fopen ("heapuri.in","r");
    FILE *fout=fopen ("heapuri.out","w");
    int n,elem,i,cer,x,poz;
    fscanf (fin,"%d",&n);
    elem=0;
    for (i=1;i<=n;i++){
        fscanf (fin,"%d",&cer);
        //printf ("%d ",cer);
        if (cer==3)
            fprintf (fout,"%d\n",h[inc]);
        // ind= pozitia in hh a elem inserat al i-lea
        // in h tii minte elem dupa valori
        // hh[i]= al catelea elem inserat a fost h[i]
        else if (cer==2){
            fscanf (fin,"%d",&x);
            poz=ind[x];
            sterge (poz,elem);
            elem--;
        }
        else {
            elem++;
            fscanf (fin,"%d",&x);
            adauga (x,elem);
        }
    }
    return 0;
}