POJ 3253 Fence Repair(优先队列+哈夫夫曼)

版权声明:听说这里让写版权声明~~~ https://blog.csdn.net/m0_37691414/article/details/82115125

解析:简单的哈夫曼编码问题+优先队列

#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int main(){
	int n;
	while(~scanf("%d", &n)){
		priority_queue<LL, vector<LL>, greater<LL> > q;
		for(int i = 0, temp; i < n; ++i){
			scanf("%d", &temp);
			q.push(temp);
		}
		LL ans = 0;
		while(q.size() > 1){
			LL temp = q.top();
			q.pop();
			temp += q.top();
			q.pop();
			q.push(temp);
			ans += temp;
		}
		printf("%lld\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37691414/article/details/82115125