Cod sursa(job #2639483)

Utilizator BogdanTicuTicu Bogdan Valeriu BogdanTicu Data 2 august 2020 13:23:19
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>

using namespace std;

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

int heap[200001],len=0,poz[200001],k=0;

void first(int val)
{
	int current=len;
	int tata=current/2;
	heap[++len]=val;
	while(heap[tata] > heap[current])
	{
		swap(heap[tata],heap[current]);
		current=tata;
	}
//	for(int i=1;i<=len;i++)
//		cout<<heap[i]<<" ";
}

void second(int x)
{
	int current=1,a,ct;
	int t;
	for(int i=1;i<=len;i++)
		if(heap[i]==poz[x]) 
		{	t=i; break; }
	swap(heap[t],heap[len]);
	len--;
	while(heap[current]>heap[current*2] || heap[current] > heap[current*2+1])
	{
		if(heap[current*2]>heap[current*2+1])
		{
			a=heap[current*2];
			ct=current*2;
		}
		else
		{
			a=heap[current*2+1];
			ct=current*2+1;
		}
		swap(heap[current],a);
		current=ct;
	}

}

int third()
{
	out<<heap[1]<<"\n";
}

int main()
{
	int n;
	in>>n;
	for(int i=1;i<=n;i++)
	{
		int cod,x;
		in>>cod;
		if(cod==1||cod==2)
		{
			in>>x;
			poz[++k]=x;
		}
		if(cod==1)
		{
			first(x);
		}
		else if(cod==2)
			second(x);
		else if(cod==3)
			third();
/*		for(int i=1;i<=len;i++)
			cout<<heap[i]<<" ";
		cout<<endl;
*/
	}
	return 0;
}