149 荷马史诗

要求  合并k个的哈夫曼树并且取层数与树的带权路径长度和最小

思路  优先队列实现小根堆每个节点包括权值和层数。每次哈夫曼操作取最大层并将层数加一。

注意要点  首先叶节点是否排满可以插入0解决,当多个值相同时应选择最小层数;

代码如下

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, int> PLI;
int main(){
    int n, k;
    cin >> n >> k;
    priority_queue<PLI, vector<PLI>, greater<PLI>> heap;
    for(int i = 0;i < n;i++){
        LL x;
        cin >> x;
        heap.push({x, 0});//从零层开始
    }
    while((n - 1) % (k - 1)) heap.push({0LL,0}), n ++;//若是不能整除就补零可以免去特判
    LL res = 0;
    while(heap.size() > 1){
        LL sum = 0;
        int depth = 0;
        for(int i = 0;i < k;i ++){//这里小根堆以first为第一关键字second为第二关键字所以取层数最小的要求已被小根堆解决
            auto x = heap.top();
            sum += x.first;
            heap.pop();
            depth = max(depth,x.second);
        }
        heap.push({sum, depth + 1});//每次结合层数加一
        res += sum;
    }
    cout << res << endl << heap.top().second << endl;
    return 0;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值