堆排全部RE,求调
查看原帖
堆排全部RE,求调
1416871
codelate楼主2024/11/2 15:48
#include<bits/stdc++.h>
using namespace std;
int heap[100000],top=0;

void push(int x){
	top++;
	int p=top;
	while(p>1){
		if(heap[p/2]<x)break;
		heap[p]=heap[p/2];
		p/=2;
	}
	heap[p]=x;
}

int pop(){
	heap[0]=heap[1];
	int x=heap[top--];
	int fa=1;
	while(fa*2<=top){
		int son=fa*2;
		if(heap[son+1]<heap[son])son++;
		if(heap[son]>x)break;
		heap[fa]=heap[son];
		fa=son;
	}
	heap[fa]=x;
	return heap[0];
}

int n,ans=0;
int main(){
	scanf("%d",&n);
	for(int i=1,a;i<=n;i++){
		scanf("%d",a);
		push(a);
	}
	while(top>1){
		int t=pop();
		t+=pop();
		ans+=t;
		push(t);
	}
	printf("%d",ans);
	return 0;
}

全re,求调

2024/11/2 15:48
加载中...