11个TEL求调
查看原帖
11个TEL求调
1200551
yuan2795楼主2024/8/22 13:36

#include<bits/stdc++.h>

#define f(x) ((x)/2)//父节点
#define g(x) ((x)*2)//哥哥

#define d(x) ((x)*2+1)//弟弟

using namespace std;

int siz;
int heap[1000001];
void push(int x)
{
	int pos=siz++;
	while(pos>0)
	{
		if(heap[f(pos)]<=x)
		{
			break;
		}
		heap[pos]=heap[f(pos)],pos=f(pos);
	}
	heap[pos]=x;
	return ;
	
	
	
}
void pop()
{
	int pos=0,x=heap[--siz];
	while(g(pos)<siz)
	{
		int t=(heap[g(pos)]<heap[d(pos)])?g(pos): d(pos);//找较小的兄弟
		if(heap[t]>=x)
		{
			break;
		}
		heap[pos]=heap[t],pos=t;
	}
	heap[pos]=x;
	return ;
}
int top()
{
	return heap[0];
}
int n,op,x;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	cin>>n;
	while(n--)
	
	
	{

		cin>>op;
		if(op==1)
		{

			cin>>x;
			push(x);
		}
		if(op==2)
		{

			cout<<top()<<endl;
		}
		if(op==3)
		{
			pop();	
		}
	}
	return 0;
}
2024/8/22 13:36
加载中...