Cod sursa(job #1200745)

Utilizator teodor440Teodor Tonghioiu teodor440 Data 23 iunie 2014 14:40:06
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

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

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

void schimba(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){
        schimba(v[y],v[y/2]);
        schimba(t[y],t[y/2]);
        y/=2;
    }
}

	void sift(int k){
		int b=1,aux,x,m;
		while(b==1){
			m=0;
			b=0;
			x=0;
			if(2*k<=n&&v[2*k]<v[k]&&v[2*k]>m){
				x=-1;
				m=v[2*k];
			}
			if(2*k+1<=n&&v[2*k+1]<v[k]&&v[2*k+1]>m) x=1;
			if(x==1) {
				aux=v[k];
				v[k]=v[2*k+1];
				v[2*k+1]=aux;
				k=2*k+1;
				schimba(t[k],t[2*k+1]);
				b=1;
			}
			else if(x==-1){
				aux=v[k];
				v[k]=v[2*k];
				v[2*k]=aux;
				k=2*k;
                schimba(t[k],t[2*k+1]);
				b=1;
			}
		}
	}

void elimina(int x){
    int i,y;
    for(i=1;t[i]!=x;i++){}
    schimba(v[i],v[n]);
    schimba(t[i],t[n]);
    y=i;
    n--;
    sift(i);
}

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;
}