Hyperhuffman

  You might have heard about Huffman encoding - that is the coding system that minimizes the expected length of the text if the codes for characters are required to consist of an integral number of bits.

  Let us recall codes assignment process in Huffman encoding. First the Huffman tree is constructed. Let the alphabet consist of N characters, i-th of which occurs Pitimes in the input text. Initially all characters are considered to be active nodes of the future tree, i-th being marked with Pi. On each step take two active nodes with smallest marks, create the new node, mark it with the sum of the considered nodes and make them the children of the new node. Then remove the two nodes that now have parent from the set of active nodes and make the new node active. This process is repeated until only one active node exists, it is made the root of the tree.

  Note that the characters of the alphabet are represented by the leaves of the tree. For each leaf node the length of its code in the Huffman encoding is the length of the path from the root to the node. The code itself can be constrcuted the following way: for each internal node consider two edges from it to its children. Assign 0 to one of them and 1 to another. The code of the character is then the sequence of 0s and 1s passed on the way from the root to the leaf node representing this character.

  In this problem you are asked to detect the length of the text after it being encoded with Huffman method. Since the length of the code for the character depends only on the number of occurences of this character, the text itself is not given - only the number of occurences of each character. Characters are given from most rare to most frequent.

  Note that the alphabet used for the text is quite huge - it may contain up to 500 000 characters.

  This problem contains multiple test cases!

  The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

  The output format consists of N output blocks. There is a blank line between output blocks.

Input

  The first line of the input file contains N - the number of different characters used in the text (2 <= N <= 500 000). The second line contains N integer numbers Pi- the number of occurences of each character (1 <= Pi <= 109, Pi <= Pi+1 for all valid i).

Output

  Output the length of the text after encoding it using Huffman method, in bits.

Sample Input

  1

  3
  1 1 4

Sample Output

  8

解题思路:
  本题给出多组数据每组数据包括测试数据数量,每个测试包括一个整数代n表字符数量(即哈夫曼树叶子结点数),n个整数代表每个字符出现的次数(即哈夫曼树叶子结点权值),要求输出其哈夫曼编码的长度。

样例解析:
  1    ->测试数据组数

  3    ->字符数量(叶子结点数)
  1 1 4   ->每个字符出现次数(哈夫曼树叶子结点权值)

#include <bits/stdc++.h>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long> > value; //优先队列数值小的先出队
int main()
{
int t;
while(cin >> t) //输入测试数据组数
{
while(t--)
{
int n; //n记录字符数量
long long ans = 0LL;
while(!value.empty()) //清空队列
{
value.pop();
}
scanf("%d", &n);
for(int i = ; i < n; i++)
{
long long temp; //输入出现次数
scanf("%lld", &temp);
value.push(temp); //出现次数入队
}
while(value.size() > )
{
long long x1 = value.top();
value.pop();
long long x2 = value.top();
value.pop();
//每次出队两个最小的数据合并后入队
ans += x1 + x2; //记录长度
value.push(x1 + x2);
}
printf("%lld\n", ans); //输出长度
if(t)
printf("\n");
}
}
return ;
}

sgu-203 Hyperhuffman(哈夫曼编码)的更多相关文章

  1. 哈夫曼(huffman)树和哈夫曼编码

    哈夫曼树 哈夫曼树也叫最优二叉树(哈夫曼树) 问题:什么是哈夫曼树? 例:将学生的百分制成绩转换为五分制成绩:≥90 分: A,80-89分: B,70-79分: C,60-69分: D,<60 ...

  2. (转载)哈夫曼编码(Huffman)

    转载自:click here 1.哈夫曼编码的起源: 哈夫曼编码是 1952 年由 David A. Huffman 提出的一种无损数据压缩的编码算法.哈夫曼编码先统计出每种字母在字符串里出现的频率, ...

  3. 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  4. HDU2527 哈夫曼编码

    Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. *HDU1053 哈夫曼编码

    Entropy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  6. YTU 3027: 哈夫曼编码

    原文链接:https://www.dreamwings.cn/ytu3027/2899.html 3027: 哈夫曼编码 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 ...

  7. 使用F#来实现哈夫曼编码吧

    最近算法课要求实现哈夫曼编码,由于前面的问题都是使用了F#来解决,偶然换成C#也十分古怪,报告也不好看,风格差太多.一开始是打算把C#版本的哈夫曼编码换用F#来写,结果写到一半就觉得日了狗了...毕竟 ...

  8. 赫夫曼\哈夫曼\霍夫曼编码 (Huffman Tree)

    哈夫曼树 给定n个权值作为n的叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree).哈夫曼树是带权路径长度最短的树,权值较大的结点离 ...

  9. hdu2527哈夫曼编码

    /* Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

随机推荐

  1. D3.js的基础部分之数组的处理 映射(v3版本)

    映射(Map)   映射(Map)是十分常见的一种数据结构,由一系列键(key)和值(value)组成的.每个key对应一个value,根据key可以获取和设定value,也可以根据key来查询val ...

  2. vscode怎样新建项目

    首先,vscode本身没有新建项目的选项,所以要先创建一个空的文件夹喔.   然后打开vscode,再在vscode里面打开文件夹,这样才可以创建项目.   选择之前创建的空文件将作为vscode的文 ...

  3. .net core 使用 redis

    .net core 使用 redis 个人感觉.net core 对于微软技术而言有很重要的意义 ,所以最近已有时间就想看一看关于.net core 的文章. 今天我就来写一写如何在.net core ...

  4. c#中sealed修饰符

    sealed 修饰符表示密封 用法: 1.用于类时,表示该类不能再被继承,不能和abstract同时使用,因为这两个修饰符在含义上互相排斥 2.用于方法和属性时,表示该方法或属性不能再被重写,必须和o ...

  5. JS三个编码函数和net编码System.Web.HttpUtility.UrlEncode比较

    JS三个编码函数和net编码比较 总结 1.escape.encodeUri.encodeUriComponent均不会对数字.字母进行编码.2.escape:对某些字符(如中文)进行unicode编 ...

  6. vuejs 添加事件时出现TypeError: n.apply is not a function

    vuejs项目中给表单元素添加事件时出现了TypeError: n.apply is not a function的错误,后来发现错误原因时处理事件的函数名和data中定义的变量名相同 当给事件添加处 ...

  7. 如何在linux设置回收站 - 防止失误操作造成数据清空

    linux rm命令是即刻删除的,而且挺多人喜欢加上-f强制命令,更暴力的是删除文件夹直接 rm -rf ,这样子代表你执行完后,就完全被干掉了. 还是推荐在linux下设置回收站,写一个shell脚 ...

  8. 【OCP-12c】CUUG 071题库考试原题及答案解析(22)

    5.choose the best answer Evaluate the following CREATE SEQUENCE statement: CREATE SEQUENCE seq1 STAR ...

  9. 【文文殿下】快速傅里叶变换(FFT)学习笔记

    多项式 定义 形如\(A(x)=\sum_{i=0}^{n-1} a_i x^i\)的式子称为多项式. 我们把\(n\)称为该多项式的次数界. 显然,一个\(n-1\)次多项式的次数界为\(n\). ...

  10. 洛谷P1393 动态逆序对(CDQ分治)

    传送门 题解 听别人说这是洛谷用户的双倍经验啊……然而根本没有感觉到……因为另外的那题我是用树状数组套主席树做的……而且莫名其妙感觉那种方法思路更清晰(虽然码量稍稍大了那么一点点)……感谢Candy大 ...