回归 洛谷P1090合并果子 [NOIP2004 提高组]

学业原因停更N月[doge]

#include<iostream>//牢大
#include<cstring>
using namespace std;

template<typename item>
class smallest_heap{
	private:
		item heap[10001];
		int len;
	public:
		smallest_heap();
		void push(item const &);
		void pop();
		item top();
		int size();
		bool empty();
		
};

template<typename item>
smallest_heap<item>::smallest_heap(){
	len=0;
	memset(heap,0,sizeof(heap));
}

template<typename item>
void smallest_heap<item>::push(item const &n){
	heap[++len]=n;
	int son=len,father=son/2;
	while(heap[son]<heap[father] && father>=1){
		swap(heap[son],heap[father]);
		son=father,father=son/2;
	}
}

template<typename item>
void smallest_heap<item>::pop(){
	swap(heap[1],heap[len]);
	heap[len--]=0;
	int father=1,son=2;
	while(son<=len){
		if(son<len && heap[son]>heap[son+1]) son++;
		if(heap[father]>heap[son]){
			swap(heap[father],heap[son]);
			father=son,son=father*2;
		}else break;
	}
}

template<typename item>
item smallest_heap<item>::top(){
	return heap[1];
}

template<typename item>
int smallest_heap<item>::size(){
	return len;
}

template<typename item>
bool smallest_heap<item>::empty(){
	return len;
}


smallest_heap<int> h;
int n,ans;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int a;
		cin>>a;
		h.push(a);
	}
	while(h.size()>1){
		int x=h.top(); h.pop();
		int y=h.top(); h.pop();
		h.push(x+y);
		ans+=x+y;
	}
	cout<<ans;
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值