题解 | #哈夫曼树#

哈夫曼树

https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155

#include <functional>
#include <iostream>
#include <vector>
#include "queue"
using namespace std;
priority_queue<int, vector<int>, greater<int>> heap;
int n;
int answer;
int main() {
    cin >> n;
    while(n--){
        int x;
        cin >>x;
        heap.push(x);
    }
    while(heap.size()>1){
        int l,r;
        l=heap.top();
        heap.pop();
        r=heap.top();
        heap.pop();
        answer+=l+r;
        heap.push(l+r);    //  哈夫曼 的 带权路径和 就等于所有非叶节点之和   也就是 l+r  之和
    }
    cout << answer <<endl;


}
// 64 位输出请用 printf("%lld")

 哈夫曼 的 带权路径和 就等于所有非叶节点之和   也就是 l+r  之和

    }

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务