题解 | #搬水果#

搬水果

https://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a

小根堆求解哈夫曼树问题

#include <iostream>
#include "queue"
using namespace std;

int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        // cout << a + b << endl;
        if (n == 0) break;
        priority_queue<int, vector<int>, greater<int>> myQueue;
        while (n--) {
            int temp;
            cin >> temp;
            myQueue.push(temp);
        }
        int ans = 0;
        while (myQueue.size() >= 2) {
            int a = myQueue.top();
            myQueue.pop();
            int b = myQueue.top();
            myQueue.pop();
            ans += a + b;
            myQueue.push(a + b);
        }
        cout<<ans<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
简历当中有水分算不算造假...
点赞 评论 收藏
分享
05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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