求调,0?
查看原帖
求调,0?
1399367
syex_WXy楼主2024/12/17 08:41
#include<bits/stdc++.h>
using namespace std;
const int MaxN=30005;
int help[MaxN],ans,size;
void down(int pos){
	int son;
	while(pos*2<=size){
		son=pos*2;
		if(son+1<=size&&help[son+1]<help[son])son++;
		if(help[son]<help[pos]){
			swap(help[son],help[pos]);
			pos=son;
		}else break;
	}
}
void make(int n){
	size=n;
	for(int i=n/2;i;i--){
		down(i);
	}
}
int Min(){
	int res=help[1];
	help[1]=help[size--];
	return res;
}
void up(int pos){
	while(pos!=1){
		if(help[pos]<help[pos/2]){
			swap(help[pos],help[pos/2]);
			pos=pos/2;
		}else break;
	}
}
void insert(int x){
	help[++size]=x;
	up(size);
}
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&help[i]);
	make(n);
	for(int i=1;i<n;i++){
		int x=Min(),y=Min();
		ans+=x+y;
		insert(x+y);
	}
	printf("%d",ans);
	return 0;
} 

求助...

2024/12/17 08:41
加载中...