CSP前打堆模板,打错了,找不出来错误,求助!
查看原帖
CSP前打堆模板,打错了,找不出来错误,求助!
261262
WaltVBAlston楼主2020/11/6 19:21
#include<iostream>
using namespace std;
int heap[100005],q,n;
void put(){
	int x=n;
	while(x>1){
		int temp=x/2;
		if(heap[temp]>heap[x])
			swap(heap[temp],heap[x]);
		else
			return;
		x=temp;
	}
	return;
}
void del(){
	int x=1;
	while(x<=n){
		int temp=x*2;
		if(heap[temp+1]<heap[temp])
			temp++;
		if(heap[temp]<heap[x])
			swap(heap[temp],heap[x]);
		else
			return;
		x=temp;
	}
	return;
}
int main(){
	cin>>q;
	while(q--){
		int t,a;
		cin>>t;
		if(t==1){
			cin>>a;
			heap[n+1]=a;
			n++;
			put();
		}
		if(t==2){
			cout<<heap[1]<<endl;
		}
		if(t==3){
			heap[1]=heap[n];
			n--;
			del();
		}
	}
	return 0;
}

如能发现并告知。感激不尽。

2020/11/6 19:21
加载中...