堆排
  • 板块题目总版
  • 楼主isJason
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/1/14 14:49
  • 上次更新2023/10/24 04:17:44
查看原帖
堆排
738674
isJason楼主2023/1/14 14:49
#include <bits/stdc++.h>
using namespace std;
int heap[1000001], tmp, heap_size, son, pa, res, now;
void push(int d){
	heap[heap_size++]=d;
	while(son>1){
		pa=son/2;
		if(heap[son]>=heap[pa]) return;
		else{
			swap(heap[son], heap[pa]);
			son=pa;
		}
	}
}
int pop(){
	res=heap[1];
	heap[1]=heap[heap_size--];
	now=1;
	while(now*2<=heap_size){
		son=now*2;
		if(son<heap_size&&heap[son]>heap[son+1]){
			son++;
		}
		if(heap[now]<=heap[son]) return res;
		swap(heap[now], heap[son]);
		now=son;
	}
	return res;
}
int main(){
    cin>>heap_size;
    for(int i=1; i<=heap_size; i++){
    	cin>>tmp;
    	push(tmp);
	}
	for(int i=1; i<=heap_size; i++){
		cout<<pop()<<" ";
	}
    cout<<pop();
    return 0;
}

RE, A not allowed system call

2023/1/14 14:49
加载中...