题解 | #哈夫曼树#

哈夫曼树

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

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;cin>>n;
    vector<int>v;
    while(n--){
        int temp;cin>>temp;
        v.push_back(temp);
    }
    sort(v.begin(),v.end());
    int totalWeight = 0;
    int sum=0;
    while(v.size()!=1){
        totalWeight=v[0]+v[1];
        sum+=totalWeight;
        v.erase(v.begin()+1);
        v.erase(v.begin());
        v.push_back(totalWeight);
        sort(v.begin(),v.end());
    }
    cout<<sum<<endl;
}
// 64 位输出请用 printf("%lld")

容易想到,但是复杂度很高

全部评论

相关推荐

码农索隆:7*24,随时待命,这是去🇷🇺打仗去了啊
点赞 评论 收藏
分享
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
码农索隆:单休一个月少休息4天,一年就是48天,平时节假日,别人3天假期,单休的两天
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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