蒟蒻求助,RE#11
查看原帖
蒟蒻求助,RE#11
431265
Fresca楼主2021/5/17 21:00

如题,求助

#include <cstdio>
#include <algorithm>
using namespace std;
int heap_size;
long long heap[100001];
void put(long long x)
{
	int now,next;
	heap[++heap_size]=x;
	now=heap_size;
	while(now>1)
	{
		next=now>>1;
		if(heap[now]>=heap[next])
			return;
		swap(heap[now],heap[next]);
		now=next;
	}
}
void pop()
{
	int now,next;
	heap[1]=heap[heap_size--];
	now=1;
	while((now<<1)<=heap_size)
	{
		next=now<<1;
		if(next<heap_size&&(heap[next+1]<heap[next]))
			++next;
		if(heap[now]<=heap[next]) 
			return;
		swap(heap[now],heap[next]);
 		now=next;                      
	} 
}
int main()
{
	int op,T;
	long long temp;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&op);
		if(op==1)
		{
			scanf("%lld",&temp);
			put(temp);
		}
		else if(op==2)
			printf("%lld\n",heap[1]);
		else
			pop();
	}
	return 0;
}
2021/5/17 21:00
加载中...