手写priority_queue44
查看原帖
手写priority_queue44
345930
Gold14526楼主2022/1/26 12:45
using namespace std;
bool cmp(int x,int y)
{
	return x<=y;
}
struct priority_que{
	int a[1048577];
	int t=0;
	int size(){return t;}
	int top(){return a[1];}
	void pop()
	{
		if(t==0)return;
		int x=1;
		a[1]=a[t];
		t--;
		while(x*2<=t)
		{
			if(cmp(a[x],a[2*x]))break;
			if(x*2+1>t)
			{
				swap(a[x],a[2*x]);
				x=2*x;
				break;
			}
			if(cmp(a[2*x],a[2*x+1]))
			{
				swap(a[x],a[2*x]);
				x=2*x;
			}
			else
			{
				if(cmp(a[x],a[2*x+1]))break;
				swap(a[x],a[2*x+1]);
				x=2*x+1;
			}
		}
	}
	void push(int s)
	{
		a[++t]=a[1];
		a[1]=s;
		int x=t;
		while(x>1&&!cmp(a[x/2],a[x]))
		{
			swap(a[x],a[x/2]);
			x/=2;
		}
	}
	bool empty(){return t==0;}
	void clear(){t=0;}
}q;
int main()
{
	q.clear();
	int n;
	scanf("%d",&n);
	int op,x;
	while(n-->0)
	{
		scanf("%d",&op);
		switch(op)
		{
			case 1:
				scanf("%d",&x);
				q.push(x);
				break;
			case 2:
				printf("%d\n",q.top());
				break;
			case 3:
				q.pop();
				break;
		}
	}
	return 0;
}
2022/1/26 12:45
加载中...