【ZOJ3931 The 16th Zhejiang University Programming ContestE】【小根堆 哈夫曼树 DP】Exact Compression 建立哈弗曼树并编码0

来源:互联网 发布:和上瘾类似的网络剧 编辑:程序博客网 时间:2024/06/07 11:29

Exact Compression

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Huffman Code is a commonly used optimal prefix code. Here is a simple introduction to Huffman Coding from wikipedia.

The technique works by creating a binary tree of nodes. These can be stored in a regular array, the size of which depends on the number of symbols, S. A node can be either a leaf node or an internal node. Initially, all nodes are leaf nodes, which contain the symbol itself, the weight (frequency of appearance) of the symbol and optionally, a link to a parent node which makes it easy to read the code (in reverse) starting from a leaf node. Internal nodes contain symbol weight, links to two child nodes and the optional link to a parent node. As a common convention, bit '0' represents following the left child and bit '1' represents following the right child. A finished tree has up to n leaf nodes and n-1 internal nodes. A Huffman tree that omits unused symbols produces the most optimal code lengths.

The simplest construction algorithm uses a priority queue where the node with lowest weight is given highest priority:

  1. Create a leaf node for each symbol and add it to the priority queue.
  2. While there is more than one node in the queue:
    1. Remove the two nodes of highest priority (lowest weight) from the queue
    2. Create a new internal node with these two nodes as children and with weight equal to the sum of the two nodes' weight.
    3. Add the new node to the queue.
  3. The remaining node is the root node and the tree is complete.

For example, one day Edward wanted to send a string "aeaaaageqqqq" to his best friend Min as a gift. There are four symbols 'a', 'e', 'g', 'q' and so their weights are 5, 2, 1, 4.

Firstly Edward merged the two symbols 'e' and 'g' with lowest weights and get a node with weight of 1 + 2 = 3. Then Edward merged two nodes of weight 3 and 4 and get a node with weight 3 + 4 = 7. Finally Edward merged the last two nodes. If we distribute the prefix '0' to the smaller node, Edward can get the code of four symbols '0', '101', '100', '11'.

If we know the number of occurrences of each character in some content, can we compress it using Huffman Code and the number of '0's is exactly E? More precisely, let the number of occurrences of the i-th character be Fi and the number of '0's in its huffman code be Ci, we want to know whether there exists a specific huffman code satisfying that the sum of Fi * Ci is equal to E.

Input

There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:

The first line of each case contains a positive integer S (2 ≤ S ≤ 128), indicates the number of different symbols.

The second line contains exactly S integers Fi (1 ≤ Fi ≤ 1000), indicates the number of occurrence of each symbol.

The third line contains a non-negative integer E (0 ≤ E ≤ 108), indicates the expected number of '0's.

Output

For each case, please output "Yes" if it can be satisfied and "No" in otherwise.

Sample Input

321 3221 3345 2 1 411

Sample Output

NoYesYes

Hint

The possible huffman codes for examples:

  1. None
  2. '1', '0'
  3. '1', '001', '000', '01'

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>#include<functional>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 448001, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int casenum, casei;priority_queue<int, vector<int>, greater<int> >q;int n, m, x;bool d[2][N];void arrayDP(){d[0][0] = 1;int now = 0;int nxt = 1;int top = 0;for (int i = 1; i < n; ++i){int x = q.top(); q.pop();int y = q.top(); q.pop();q.push(x + y);for (int j = top + max(x, y); j >= 0; --j)d[nxt][j] = 0;for (int j = top; j >= 0; --j)if (d[now][j]){d[nxt][j + x] = d[nxt][j + y] = 1;gmax(top, j + x);gmax(top, j + y);}now ^= 1;nxt ^= 1;}puts(top >= m&&d[now][m] ? "Yes" : "No");}bitset<N>f;void BitsetDP(){f.reset(); f[0] = 1;for (int i = 1; i < n; ++i){int x = q.top(); q.pop();int y = q.top(); q.pop();q.push(x + y);f = (f << x) | (f << y);}puts(f[m] ? "Yes" : "No");}int main(){scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d", &n);while (!q.empty())q.pop();for (int i = 1; i <= n; ++i)scanf("%d", &x), q.push(x);scanf("%d", &m);if (m >= N){puts("No");continue;}BitsetDP();}return 0;}/*【trick&&吐槽】1,千万要注意数据规模,不要自己把自己误导了2,bitset运算确实快,但是要时刻注意到3,bitset过程运算也是会爆栈的,要注意过程也用全局数组存储4,最后的答案显然不会超过log(128)*sum(fi),即不超过896000,实际只有450000【题意】有n(128)个节点,我们要构造一棵哈夫曼树。然后给每个节点做编码。我们最后会求和——每个点的点权*根节点到这个节点路径上0的个数是否恰好为m(m∈[0,1e8])【类型】哈夫曼树 优先队列 DP【分析】虽然题目给的数值上限超级大。然而,实际上却达不到这样的值。实际上可达的数值上限只有448000于是我们可以直接用bitset暴力【时间复杂度&&优化】O(T * 128*450000)动态滚动DP上界显然是DP中非常重要也必不可少的技巧。*/


0 0
原创粉丝点击