当前位置:   article > 正文

北京邮电大学 哈夫曼树_哈夫曼树 北京邮电大学

哈夫曼树 北京邮电大学
#include<bits/stdc++.h>
#include<queue>
using namespace std;
int main(){
    int n,x;
    priority_queue<int,vector<int>,greater<int> >q;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%d",&x);
            q.push(x);  
        }
        int ans=0;
        while(q.size()>1){
            int x1=q.top();
            q.pop();
            int x2=q.top();
            q.pop();
            ans+=(x1+x2);
            q.push(x1+x2);
        }
        printf("%d\n",ans); 
    }
    return 0;   
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/596019
推荐阅读
相关标签
  

闽ICP备14008679号