4198: [Noi2015] Homer Epic
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 127 Solved: 80
Description
追逐影子的人,自己就是影子。 ——荷马
Allison has recently been fascinated by literature. She likes to have a cappuccino on a lazy afternoon and read the story of Homer in her own way. But the monumental work Homer Epic, made up of the Odyssey and the Iliad, is too long, and Allison wants to make it shorter by a coding method.
There are n different words in Homer's epic, numbered from 1 to N. The total number of occurrences of the first word I was wi. Allison wants to replace the word I with the K-binary string si, which satisfies the following requirements:
对于任意的 1≤i,j≤n,i≠j,都有:si 不是 sj 的前缀。
Allison now wants to know how to choose Si to make the new Homer epic the least long. Allison also wants to know the shortest length of the longest si in the case that the total length is minimal.
A string is called a K-binary string, when and only if its characters are 0 to k?1 (including 0 and k?1) integers.
The string Str1 is called the prefix of the string Str2, when and only if: 1≤t≤m exists, making str1=str2[1..t]. where m is the length of the string Str2, str2[1..t] represents the string consisting of the first T characters of STR2.
Input
The 1th line of the input file contains 2 positive integer n,k, separated by a single space, to represent a total of n words, which need to be replaced with a K-binary string.
Next n lines, line i+1 contains 1 non-negative integer WI, indicating the number of occurrences of the first word.
Output
The output file consists of 2 rows.
Line 1th outputs 1 integers, the shortest length after the "Homer Epic" has been re-encoded.
The 2nd line outputs 1 integers, the shortest length of the longest string si, in case the shortest total length is guaranteed.
Sample Input
4 21122
Sample Output
122
HINT
X (k) indicates that x is a string in K-binary notation.
An optimal scheme: 00 (2) replaces the 1th Word, 01 (2) replaces the 2nd Word, 10 (2) replaces the 3rd Word, and 11 (2) replaces the 4th Word. In this scenario, the shortest length after encoding is:
1x2+1x2+2x2+2x2=12
The length of the longest string Si is 2.
A non-optimal scenario: 000 (2) replaces the 1th Word, 001 (2) replaces the 2nd Word, 01 (2) replaces the 3rd Word, and 1 (2) replaces the 4th Word. In this scenario, the shortest length after encoding is:
1x3+1x3+2x2+2x1=12
The length of the longest string Si is 3. The length of the article is the same as the optimal scheme, but the longest string length is longer.
For all data, guarantee 2≤n≤100000,2≤k≤9.
Players note that 64-bit integers are used for input, output, storage, and calculation.
Exercises
If you do not think Huffman on the knees.
Analysis of the problem, found that the number of occurrences is actually a weight, we require a coding scheme so that the weight x length and the smallest, can be directly set K fork Huffman tree mess.
The analogy merges the fruit, considering the coding backwards. Start coding from the leaf node, each time the minimum k number is taken to synthesize an X, and then encode the length plus 1 (which is actually inverted, but the same), then throw x back, and so on.
Attention:
There may be empty nodes, that is, there may be a node son less than K, which need to fill 0 to prevent errors, commonly used judgment method: (n-1)% (k-1), equals 0 is full K fork tree.
(Because for a tree full of k, any node has a K son, or no son, set leaf nodes have X, then (n-x) *k=n-1, deduction can be)
Code:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <queue>#include <algorithm>using namespace STD;#define LL Long Longstructh{LL W;intLBOOL operator< (ConstH &a)Const{if(W!=A.W)returna.w<w;returna.l<l; }};p riority_queueintN,k,nn; LL ans=0;intIn () {intx=0;CharCh=getchar (); while(ch<' 0 '|| Ch>' 9 ') Ch=getchar (); while(ch>=' 0 '&& ch<=' 9 ') x=x*Ten+ch-' 0 ', Ch=getchar ();returnx;} ll Lin () {ll x=0;CharCh=getchar (); while(ch<' 0 '|| Ch>' 9 ') Ch=getchar (); while(ch>=' 0 '&& ch<=' 9 ') x=x*Ten+ (LL) (ch-' 0 '), Ch=getchar ();returnx;}intMain () {n=in (), K=in (); nn=n; for(intI=1; i<=n; i++) {LL x=lin (); H.push ((H) {x,1}); }if((n1)% (K-1)) nn+= (K-1)-((n1)% (K-1)); for(inti=n+1; i<=nn; i++) H.push ((h) {0,1}); while(nn>1) {LL s1=0;intS2=0; for(intI=1; i<=k; i++) {H x=h.top (); H.pop (); S1+=X.W; S2=max (S2,X.L); } ans+=s1; nn-= (K-1); H.push ((H) {s1,s2+1}); }printf("%lld\n%d\n", Ans,h.top (). L1);return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Huffman Tree" "Greedy" "NOI 2015" "Bzoj 4198" Homer Epic