Cod sursa(job #1813646)

Utilizator sotoc1999Sotoc George sotoc1999 Data 23 noiembrie 2016 09:16:42
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int n,r,x;
int h[200001],nr;
int v[200001],nralt;
void adaug(int i){
    int poz=i;
    while(poz>1 && h[poz/2]>h[poz]){
        int t=h[poz/2];
        h[poz/2]=h[poz];
        h[poz]=t;
    }
}
void elimx(int &n,int x){
    h[x]=h[n];
    n--;
    int poz=x;
    while(poz<=n){
        if(poz*2<=n){
            int fiu=2*poz;
            if(fiu+1<=n && h[fiu+1]<h[fiu])
                fiu++;
            if(h[fiu]<h[poz]){
                int t=h[poz];
                h[poz]=h[fiu];
                h[fiu]=t;
                t=v[poz];
                v[poz]=v[fiu];
                v[fiu]=t;
                poz=fiu;
            }
            else
                poz=n+1;
        }
        else
            poz=n+1;
    }
    v[nr]=0;
}
int main()
{
    in>>n;
    for(int i=1;i<=n;++i){
        in>>r;
        if(r==1){
            in>>x;
            ++nr,++nralt;
            h[nr]=x;
            v[x]=nr;
            adaug(nr);
        }
        else if(r==2){
            in>>x;
            elimx(nr,v[x]);
        }
        else{
            out<<h[1]<<"\n";
        }
    }
    return 0;
}