题目链接:https://vjudge.net/problem/UVA-12676

题目大意

  一串文本中包含 N 个不同字母,经过哈夫曼编码后,得到这 N 个字母的相应编码长度,求文本的最短可能长度。

分析

  哈夫曼树有这样一个性质,对于位于第 i 层的节点 A 和 第 i + 1 层的节点 B,A 出现的频率肯定大于等于 B,不然就可以把这两个节点互换,可以得到更优的哈夫曼树,因此,A 所能取到的最小频率,就是 i + 1 层所有节点出现频率的最大值,而根据题意,哈夫曼树的最底层节点频率可以全部设置为 1,如此可以一层层往上递推。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef vector< VI > VVI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, PII > MIPII;
typedef map< string, int > MSI;
typedef multimap< int, int > MMII;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e4 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Node{
LL weight = , len; bool operator< (const Node &x) const {
if(len == x.len) return weight > x.weight;
return len < x.len;
}
}; int N; int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
while(cin >> N) {
priority_queue< Node > maxH;
Rep(i, N) {
Node t;
cin >> t.len;
maxH.push(t);
} // nowLen: 记录当前处理第几层
// nowMax: 记录 i + 1 层的最大值
// tmpMax: 记录当前层 i 层的最大值
LL nowLen = maxH.top().len, nowMax = , tmpMax = -;
while(maxH.size() > ) {
Node a = maxH.top(); maxH.pop();
Node b = maxH.top(); maxH.pop(); if(a.len == nowLen) {
// 代码看到这里也许会有疑问,就是如果 a.weight == 0 或是 b.weight == 0,难道不要把节点扔回去重新取吗?
// 其实不需要,因为它们就是最小的
// 设max(i)表示倒数第 i 层的频率最大值,min(i)表示倒数第 i 层的频率最小值
// 容易看出 max(i) <= 2^i, min(i) >= 2^{i-1}
// 于是倒数第 i + 1 层权值为 0 的节点会被赋值为 max(i)
// 而那些权值不为 0 的节点,权值必然大于等于2倍的min(i),为 2^i,大于等于 max(i)
if(a.weight == ) a.weight = nowMax;
if(b.weight == ) b.weight = nowMax;
tmpMax = max(tmpMax, b.weight); a.weight += b.weight;
--a.len;
maxH.push(a);
}
else {
nowLen = a.len;
nowMax = tmpMax;
tmpMax = -;
maxH.push(a);
maxH.push(b);
}
}
cout << maxH.top().weight << endl;
}
return ;
}

UVA 12676 Inverting Huffman的更多相关文章

  1. The 2013 South America/Brazil Regional Contest 题解

    A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...

  2. UVa 10954 (Huffman 优先队列) Add All

    直接用一个优先队列去模拟Huffman树的建立过程. 每次取优先队列前两个数,然后累加其和,把这个和在放入到优先队列中去. #include <cstdio> #include <q ...

  3. UVa 10954 全部相加(Huffman编码)

    https://vjudge.net/problem/UVA-10954 题意:有n个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和, ...

  4. UVA 10954 Add All 全部相加 (Huffman编码)

    题意:给你n个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. Huffman编码.Huffman编码对于着一颗二叉树,这里的数对应着单 ...

  5. 【uva 10954】Add All(算法效率--Huffman编码+优先队列)

    题意:有N个数,每次选2个数合并为1个数,操作的开销就是这个新的数.直到只剩下1个数,问最小总开销. 解法:合并的操作可以转化为二叉树上的操作[建模],每次选两棵根树合并成一棵新树,新树的根权值等于两 ...

  6. UVA 240 Variable Radix Huffman Encoding

    题目链接:https://vjudge.net/problem/UVA-240 题目大意 哈夫曼编码是一种最优编码方法.根据已知源字母表中字符出现的频率,将源字母表中字符编码为目标字母表中字符,最优的 ...

  7. UVA - 10954 Add All (全部相加)(Huffman编码 + 优先队列)

    题意:有n(n <= 5000)个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和,求最小总开销.所有数均小于10^5. 分析:按 ...

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

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

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

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

随机推荐

  1. 洛谷 2327 [SCOI2005]扫雷

    输入输出格式 输入格式: 第一行为N,第二行有N个数,依次为第二列的格子中的数.(1<= N <= 10000) 输出格式: 一个数,即第一列中雷的摆放方案数. 输入输出样例 输入样例#1 ...

  2. 十折交叉验证10-fold cross validation, 数据集划分 训练集 验证集 测试集

    机器学习 数据挖掘 数据集划分 训练集 验证集 测试集 Q:如何将数据集划分为测试数据集和训练数据集? A:three ways: 1.像sklearn一样,提供一个将数据集切分成训练集和测试集的函数 ...

  3. 静态栈-------C语言

    /***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...

  4. 用java进行测试php写的接口

    <?php /* * @Author: anchen * @Date: 2018-07-06 13:53:19 * @Last Modified by: anchen * @Last Modif ...

  5. XML XPATH simpleXML

    XPath 通过DOM结构定位节点,在数据量很大的情况下速度下降的很厉害.解决方法是XPath.Xpath的作用:用于快速定位节点 position()是节点的位置,节点的位置是从1开始 simple ...

  6. 【Linux】- 守护进程的启动方法

    转自:Linux 守护进程的启动方法 Linux中"守护进程"(daemon)就是一直在后台运行的进程(daemon). 本文介绍如何将一个 Web 应用,启动为守护进程. 一.问 ...

  7. 33-Ubuntu-用户权限-04-修改目录权限

    修改目录权限 例:test 1.可执行权限---x 减少目录x权限,无法切换到该目录 chmod -x test 2.读权限 ---r 减少目录r权限,无法读取(ls)目录信息 chmod -r te ...

  8. 11-Ubuntu-根目录下各目录的功能详细介绍

    转自: https://www.cnblogs.com/yudar/p/5809219.html 注:总结的非常详细

  9. 8u ftp 可以连接但是无法获取目录的解决办法:无法打开传输通道。原因:由于...

    来自: http://www.3566t.com/news/dlsn/1557906.html 状态:  正在取得目录列表... 命令:  CWD xinghun 响应:  250 OK. Curre ...

  10. transport error 202: bind failed: Address already in use

    background: I have terminated some test debugger without properly saying goodbye. the JDWP didn't cl ...