题目链接: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. Android编程:解决异常“android.view.InflateException: Binary XML file line # : Error inflating class”

    今天写程序发现一个问题,就是XML中报出android.view.InflateException异常,可能的原因有: 1.XML中使用到得组件名称是否书写正确(包名+类名),可以使用crtl+鼠标点 ...

  2. 网格图必经点+dfs——cf1214D

    先正着走一次把所有可行路径标记出来,然后倒着走两条路径,一条是能向下就向下的路径,另一条能向右就向右. 如果这两条路径相交,那么(1,1)-(n,m)路径上比有个必经点,把这个必经点封上,答案是1,如 ...

  3. RzGroupBar

    何分多层 procedure TForm1.FormCreate(Sender: TObject); begin RzGroup1.Items.Clear; RzGroup1.Items.Add.Ca ...

  4. NOIp2018集训test-10-22 (联考六day2)

    中间值 两个log肯定会被卡.我用的第一种做法,就是要各种特判要在两个序列都要二分比较麻烦. //Achen #include<bits/stdc++.h> #define For(i,a ...

  5. img标签+map的使用

    img标签+map的使用 img标签含有一个usemap属性,用法相当于锚点的使用,usemap="#useName".然后就是map标签,具体代码: <body> & ...

  6. spring4.3.5基本配置

    1.去官网下载必要的jar包,以及: 2.新建一个web项目,在Window->Java->Build Path->User Libraries 按照步骤1,2把spring的jar ...

  7. 使用U盘或在本地电脑作为git远程仓库进行托管

    情景描述: 当有两台电脑需要共同维护一段代码,其中一台电脑不希望(或者不能)通过网络的方式进行访问git仓库(即不使用github),那么可以使用U盘作为介质将其作为远程仓库,或者使用局域网中一台电脑 ...

  8. 从pcap文件中分析出数据包

    import dpkt import struct import sys,os f=file(sys.argv[1],"rb") pcap=dpkt.pcap.Reader(f) ...

  9. Codeforces 1169A Circle Metro

    题目链接:codeforces.com/contest/1169/problem/A 题意:有俩个地铁,一个从 1 → 2 → …→ n → 1→ 2 →…, 一个 从 n → n-1 →…→ 1 → ...

  10. 在VMware软件下创建CentOs虚拟机

    1.创建新的虚拟机. 打开VMware软件,点击主页内创建新的虚拟机 2.进入新建虚拟机向导 点击典型,点击下一步 3.在下一步中单击稍后安装操作系统 点击下一步 4.选择操作系统类型 因为CentO ...