10分555……
查看原帖
10分555……
698261
WangWang6楼主2022/12/11 11:49
#include<bits/stdc++.h>
using namespace std;
int tree[10001],heh[10000];
int N;
void aju(int root,int n){
	int N = n;  
	int rootpot = root; 
	int leftchild = rootpot * 2; 
	while (leftchild <= N)
	{
		if(leftchild + 1 <= N && tree[leftchild + 1] > tree[leftchild])
		{
			leftchild++; 
		}
		if(tree[leftchild] > tree[rootpot])
		{
			swap(tree[leftchild],tree[rootpot]); 
			rootpot = leftchild; 
			leftchild = rootpot * 2; 
		}
		else
		{
			return; 
		}
	}
} 
int main()
{
	cin>>N;  
	for(int i = 1; i <= N; i++)
	{
		cin>>tree[i]; 
	}
	for(int i = N >> 1; i >= 1; i--)
	{
		aju(i,N); 
	}//将输入的数据变成大根堆 
	
	//堆排序 
	int v=N;
	for(int i = 1; i <= N - 1; i++)
	{
		swap(tree[1],tree[N - i + 1]); 
		aju(1,--v); 
	}
	//计算
	int sum=0;
	for(int i=1;i<=N;i++)
	{
		heh[i]=tree[i]+tree[i+1];
		tree[i+1]=heh[i];
		tree[i]=0;
	}
	for(int i=1;i<=N-1;i++)
	{
		sum+=heh[i];
	}
	cout<<sum;
	return 0; 
}

2022/12/11 11:49
加载中...