各位大佬帮帮我,堆排tle了三个点【悬关】
查看原帖
各位大佬帮帮我,堆排tle了三个点【悬关】
758241
Liudongsheng1楼主2024/3/10 18:12
#include<bits/stdc++.h>
using namespace std;
int t[100010];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>t[i];
		int x=i;
		while(x>1&&t[x]<t[x/2]){
			swap(t[x],t[x/2]);
			x/=2;
		}
	}
	while(n>=1){
		swap(t[n],t[1]);
		cout<<t[n]<<' ';
		n--;
		int k=1;
		while(2*k<=n){
			if(2*k+1<=n&&t[2*k]<=t[(2*k)+1]&&t[k]>=t[2*k]||2*k+1>n&&t[k]>=t[2*k]){
				swap(t[k],t[2*k]);
				k*=2;
			}
			else if(2*k+1<=n&&t[2*k]>t[(2*k)+1]&&t[k]>t[(2*k)+1]){
				swap(t[k],t[(2*k)+1]);
				k=(2*k)+1;
			}
		}
	}
	return 0;
}
2024/3/10 18:12
加载中...