Cod sursa(job #1200731)

Utilizator teodor440Teodor Tonghioiu teodor440 Data 23 iunie 2014 14:17:05
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

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

int v[200001],t[200001],n=0,o=1;

void swap(int &x,int &y){
    int aux;
    aux=x;
    x=y;
    y=aux;
}

void push(int x){
    int y;
    n++;
    v[n]=x;
    t[n]=o;
    o++;
    y=n;
    while(v[y/2]>v[y]&&y>1){
        swap(v[y],v[y/2]);
        swap(t[y],t[y/2]);
        y/=2;
    }
}

void elimina(int x){
    int i,y;
    for(i=1;t[i]!=x;i++){}
    swap(v[i],v[n]);
    swap(t[i],t[n]);
    y=i;
    if(i!=1)
    while(v[y/2]>v[y]&&y>1){
        swap(v[y],v[y/2]);
        swap(t[y],t[y/2]);
        y/=2;
    }
    else{
    while(v[2*y]<v[y]&&2*i<n){
        swap(v[y],v[y*2]);
        swap(t[y],t[y*2]);
        y/=2;
    }
    while(v[2*y+1]<v[y]&&2*y+1<n){
        swap(v[y],v[y/2+1]);
        swap(t[y],t[y/2+1]);
        y/=2;
    }

    }
    n--;
}

int main(){
    int i,x,c;
    f>>x;
    for(i=1;i<=x;i++){
        f>>c;
        if(c==3) g<<v[1]<<endl;
        else if(c==1) {
            f>>c;
            push(c);
        }
        else{
            f>>c;
            elimina(c);
        }
        for(int j=1;j<=n;j++) cout<<v[j]<<" ";cout<<endl;
        for(int j=1;j<=n;j++) cout<<t[j]<<" ";cout<<endl<<endl;
    }

    return 0;
}