九度oj 题目1172:哈夫曼树
- 题目描述:
-
哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。
- 输入:
-
输入有多组数据。
每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。
- 输出:
-
输出权值。
- 样例输入:
-
5
1 2 2 5 9
- 样例输出:
-
37 权值为层数,从0开始,一开始本来想用数组来构造哈夫曼树,代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm> #define MAX 1009
#define HMAX 2009 struct Haff
{
int level;
int key;
bool isLeaf;
};
Haff haff[MAX]; int cmp(const void* a, const void *b) {
Haff at = *(Haff *)a;
Haff bt = *(Haff *)b;
if(at.key != bt.key)
return at.key - bt.key;
else if(at.isLeaf == bt.isLeaf) {
return ;
}
else {
if(at.isLeaf == true) {
return ;
}
else {
return -;
}
}
} int max(int a, int b) {
return a > b ? a: b;
}
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
scanf("%d",&haff[i].key);
haff[i].level = ;
haff[i].isLeaf = true;
}
int ptr = ;
int count = n;
int cen;
int N = * n - ;
while(ptr < N - ) {
qsort(&haff[ptr],count-ptr, sizeof(Haff), cmp); int pj = ptr + ;
cen = max(haff[ptr].level, haff[pj].level);
haff[ptr].level = cen;
haff[pj].level = cen;
haff[count].key = haff[ptr].key + haff[pj].key;
haff[count].level = cen + ;
haff[count].isLeaf = false;
count++;
ptr = ptr + ;
} int sum = ;
cen = haff[N-].level; /*for(int i = 0; i < count; i++) {
printf("%d %d\t",haff[i].key, haff[i].level);
}
printf("\n");*/
for(int i = ; i < count; i++) {
if(haff[i].isLeaf == true) {
sum = sum + (cen - haff[i].level) * haff[i].key;
}
}
printf("%d\n",sum);
}
return ;
}但这样做,level的值会出现错误。
后来发现去计算答案根本不需要建树,代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm> #define MAX 1009 int haff[MAX]; int cmp(const void* a, const void *b) {
int at = *(int *)a;
int bt = *(int *)b;
return at - bt; } int max(int a, int b) {
return a > b ? a: b;
}
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
scanf("%d",&haff[i]);
}
int ptr = ;
int count = n;
int N = * n - ;
int sum = ;
while(ptr < N - ) {
qsort(&haff[ptr],count-ptr, sizeof(int), cmp); int pj = ptr + ;
haff[count] = haff[ptr] + haff[pj];
sum = sum + haff[count];
count++;
ptr = ptr + ;
}
printf("%d\n",sum);
}
return ;
}
九度oj 题目1172:哈夫曼树的更多相关文章
- 九度oj 题目1088:剩下的树
题目描述: 有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
随机推荐
- CF1080D Olya and magical square
思路: 构造. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; ll sum[]; int ...
- css选择器(基础)
CSS选择器: 一个样式的语法是由选择器+属性+属性值三部分组成: 到底什么是选择器呢? 答:个人直白的理解为:选择一种要对它进行操作的标签的方法就叫做选择器.也就是说选择器就是一种选择元素的 ...
- TitleBar(标题栏)的使用
github地址:https://github.com/buhuiming/BHMAndroid 使用compile 'com.bhm.sdk.bhmlibrary:BHMLibrary:1.1.2' ...
- COGS 36. 求和问题
时间限制:1.2 s 内存限制:128 MB [问题描述] 在一个长度为n的整数数列中取出连续的若干个数,并求它们的和. [输入格式] 输入由若干行组成,第一行有一个整数n ...
- [Q&A]VS 2012 MVC4专案与网站的差异?「ASP.NET组态」的Login账号出现在「新旧两组」会员数据库里面?
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/30/mvc4_vs2012_login_member_db.aspx [Q&a ...
- js 判断是什么浏览器、是否为谷歌浏览器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- 中国剩余定理&Lucas定理&按位与——hdu 5446
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...
- div+css实现几种经典布局的详解
一.左右两侧,左侧固定宽度200px,右侧自适应占满 <div class="divBox"> <div class="left">&l ...
- 面向对象编程OOP-2
用ES6的方法 实现类的继承 //类的定义 class Animal { //ES6中新型构造器 constructor(name,age) { this.name = name; this.age= ...
- laydate控件后台返回的时间前台格式化
//功能:laydate控件后台返回的时间前台格式化 //参数:laydate控件值 function formatDate(strTime) { if ("" === strTi ...