//Inverting Huffman(静态哈夫曼)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=55;
vector<long long> deep[maxn];
int n,x;
int main()
{
while(cin>>n){
for(int i = 0;i < n; i++){
deep[i].clear();
}
int maxd=0;
for(int i = 0; i < n; i++){
cin >> x;
deep[x].push_back(0);
maxd = max(maxd, x);//求最大深度
}
long long temp = 1;
for(int i = maxd; i > 0;i--){
for(int j=0;j<deep[i].size();j++){
if(!deep[i][j]){
deep[i][j] = temp;
}
}
sort(deep[i].begin(),deep[i].end());
for(int j=0;j<deep[i].size(); j+=2){
deep[i - 1].push_back(deep[i][j] + deep[i][j + 1]);
}
temp = *(deep[i].end() - 1);
}
cout << *(deep[0].begin()) << endl;
}
return 0;
}
Inverting Huffman(静态哈夫曼)
最新推荐文章于 2024-08-30 19:42:47 发布