poker card game

POJ

题面懒得放了,就是一道哈夫曼树的模板题,而且还是2叉的,直接丢代码了.

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
inline int read(){
    int x=0,o=1;char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')o=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*o;
}
priority_queue<int,vector<int>,greater<int> >q;
inline void Huffman(int n){
	int ans=0;
	while(n>1){
		int tot=0;
		for(int i=1;i<=2;++i){
			tot+=q.top();q.pop();
		}
		q.push(tot);ans+=tot;n--;
	}
	printf("%d
",ans);
}
int main(){
	int T=read();
	while(T--){
		while(q.size())q.pop();
		int n=read();
		for(int i=1;i<=n;++i){int a=read();q.push(a);}
		Huffman(n);
	}
    return 0;
}

原文地址:https://www.cnblogs.com/PPXppx/p/11250469.html