哈夫曼树——北京邮电大学复试上机

纯模板

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int main(){
    int n;
    priority_queue<int,vector<int>,greater<int> > q;
    while(scanf("%d",&n) != EOF){
    while(!q.empty()){
        q.pop();    
    } 
    int x,y,ans = 0,tmp;
    for(int i = 0;i < n;i++){
        scanf("%d",&tmp);
        q.push(tmp);
    }
    while(q.size() > 1) { //保证至少有两个元素在队列中 
    x = q.top();
    q.pop();
    y=q.top();
    q.pop();
    ans+=x+y;
    q.push(x+y);
    }   
        printf("%d\n",ans);
    }
    return 0;
} 
全部评论

相关推荐

昨天 18:37
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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