题目大意:给定n个叶节点的权值,请建立哈夫曼树,并求出每个叶节点的路径长与权值乘积之和。 路径长指叶节点到根节点的长度。
还是一样的,我们需要来分析样例,不分析样例一上来就开始写题是不成熟的,所以,开始分析吧:
11-1 哈夫曼树
这里我将不是构造的数字用了圈来表示,你发现了没有37=3+5+10
+19!!
这说明了什么,我们只需要循环然后每次排序,前两个数字相加存到第一个数字里面,取第一个数字!这样就可以了。

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<unordered_map>
#include<queue>
#include<limits>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int>temp(n);
    for(int i=0;i!=n;++i)
        cin>>temp[i];
    int res=0;
    for(int i=0;i!=n-1;++i)
    {
        sort(temp.begin()+i,temp.end());
        temp[i+1]+=temp[i];
        res+=temp[i+1];
    }
    cout<<res<<endl;
}

相关文章:

  • 2021-07-20
猜你喜欢
  • 2021-12-09
  • 2022-12-23
  • 2021-05-21
  • 2021-05-27
  • 2021-05-10
  • 2021-05-28
  • 2021-12-25
相关资源
相似解决方案