Cod sursa(job #662283)

Utilizator bogdan353Costea Bogdan bogdan353 Data 16 ianuarie 2012 13:37:26
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include<fstream>
#include<algorithm>
using namespace std;

int n,a[100],H[100],pozh[100],dim=0;

void push(int nod)
{
	int ta=nod/2;
	
	if(nod==1) return;
	if(a[H[ta]]<=a[H[nod]]) return;
	swap(H[nod],H[ta]);
	swap(nod,ta);
	
	push(ta);
}
	
void pop(int nod)
{
	int f1=nod*2;
	int f2=nod*2+1;
	int nodmin=nod;
	
	if(nod==nodmin) return;
	
	if(a[H[f1]]<a[H[nodmin]] && f1<=dim)
		nodmin=f1;
	if(a[H[f2]]<a[H[nodmin]] && f2<=dim)
		nodmin=f2;
	
	swap(nod,nodmin);
	swap(H[nod], H[nodmin]);
	
	
	pop(nodmin);
}
		
	
int main()
{
	ifstream f("heapuri.in");
	ofstream g("heapuri.out");
	
	f>>n;
	
	int dim2=0,t,e;
	
	for(int i=1;i<=n;i++)
	{
		f>>t;
		
		if(t==1)
		{
			f>>e;
			dim2++;
			dim++;
			a[dim2]=e;
			pozh[dim]=dim;
			H[dim]=dim2;
			push(pozh[dim]);
		}
		if(t==2)
		{
			f>>e;
			swap(pozh[H[e]],pozh[H[dim]]);
			swap(H[e],H[dim]);
			dim--;
			pop(pozh[H[e]]);
			push(pozh[H[e]]);
		}
		if(t==3)
			g<<a[H[1]]<<"\n";
	}
}