Cod sursa(job #893121)

Utilizator Aleks10FMI - Petrache Alex Aleks10 Data 26 februarie 2013 13:12:47
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>

using namespace std;

int n=0,h[200000],aux,k,N,v[1000000];

    ifstream f("heapuri.in");
    ofstream g("heapuri.out");

void inserare(int x){
    n++;
    h[n]=x;
    k=n;
    while(h[k]<h[k/2] && k/2>=1){
        aux=h[k];
        h[k]=h[k/2];
        h[k/2]=aux;
        k=k/2;
    }
}

void stergere(int k){
    int min,p;
    if(2*k<=n){
        min=h[2*k];
        p=2*k;
        if(2*k+1<=n && h[2*k+1]<min){
            min=h[2*k+1];
            p=2*k+1;
        }
        aux=h[k];
        h[k]=h[p];
        h[p]=aux;
        stergere(p);
    }
}

void afiseaza(){
   /* int i;
    for(i=1;i<=n;i++){
        cout<<h[i]<<" ";
    }
    cout<<'\n';*/
    g<<h[1]<<'\n';
}

int main()
{
    int x,cod,i,j;
    f>>N;
    for (i=1;i<=N;i++){
        f>>cod;
        if(cod==1){
            f>>x;
            inserare(x);
            v[n]=x;
        }
        if(cod==2){
            f>>x;
            for(j=1;j<=n;j++){
                if(v[x]==h[j]){
                    x=j;
                    break;
                }
            }
            stergere(x);

        }
        if(cod==3){
            afiseaza();
        }
    }

   /* for(i=1;i<=n;i++){
        cout<<v[i]<<" ";
    }*/
    return 0;
}