#P2168. [NOI2015] 荷马史诗

    ID: 1147 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心2015二叉堆NOI 系列优先队列哈夫曼树哈夫曼,Huffman

[NOI2015] 荷马史诗

Description

Allison has recently become obsessed with literature. On a lazy afternoon, she likes to slowly sip a cappuccino and quietly read her beloved Homer’s Epic. However, the grand work composed of The Odyssey and The Iliad is just too long, so Allison wants to shorten it by using an encoding method.

In one Homer’s Epic, there are nn distinct words, numbered from 11 to nn. The total number of occurrences of the ii-th word is wiw_i. Allison wants to replace the ii-th word with a base-kk string sis_i such that the following condition holds:

For any 1i,jn1 \leq i, j \leq n, iji \ne j, we have: sis_i is not a prefix of sjs_j.

Now Allison wants to know how to choose sis_i to minimize the length of the new Homer’s Epic after replacement. Among all choices that minimize the total length, she also wants to know the minimal possible value of the maximum length among all sis_i.

A string is called a base-kk string if and only if each of its characters is an integer between 00 and k1k-1 inclusive.

A string str1str1 is called a prefix of a string str2str2 if and only if there exists 1tm1 \leq t \leq m such that str1=str2[1..t]str1 = str2[1..t]. Here, mm is the length of str2str2, and str2[1..t]str2[1..t] denotes the string consisting of the first tt characters of str2str2.

Input Format

The first line contains 22 positive integers n,kn, k, separated by a single space, indicating that there are nn types of words and base-kk strings are to be used for replacement.

The next nn lines: the (i+1)(i+1)-th line contains 11 positive integer wiw_i, indicating the number of occurrences of the ii-th word.

Output Format

Output consists of 22 lines.

The first line contains 11 integer, the minimal total length of Homer’s Epic after re-encoding.

The second line contains 11 integer, the minimal possible value of the maximum length among all sis_i under the condition that the total length is minimized.

4 2
1
1
2
2

12
2
6 3
1
1
3
3
9
9

36
3

Hint

Sample Explanation

Sample 1 explanation

Let X(k)X(k) denote that XX is a base-kk string.

One optimal scheme: let 00(2)00(2) replace the 11-st word, 01(2)01(2) replace the 22-nd word, 10(2)10(2) replace the 33-rd word, and 11(2)11(2) replace the 44-th word. Under this scheme, the minimal encoded length is:

1×2+1×2+2×2+2×2=121 × 2 + 1 × 2 + 2 × 2 + 2 × 2 = 12

The maximum length of sis_i is 22.

A non-optimal scheme: let 000(2)000(2) replace the 11-st word, 001(2)001(2) replace the 22-nd word, 01(2)01(2) replace the 33-rd word, and 1(2)1(2) replace the 44-th word. Under this scheme, the minimal encoded length is:

1×3+1×3+2×2+2×1=121 × 3 + 1 × 3 + 2 × 2 + 2 × 1 = 12

The maximum length of sis_i is 33. Compared with the optimal scheme, the article length is the same, but the maximum codeword length is larger.

Sample 2 explanation

One optimal scheme: let 000(3)000(3) replace the 11-st word, 001(3)001(3) replace the 22-nd word, 01(3)01(3) replace the 33-rd word, 02(3)02(3) replace the 44-th word, 1(3)1(3) replace the 55-th word, and 2(3)2(3) replace the 66-th word.

Constraints

All testdata ranges and characteristics are as follows (all data satisfy 0<wi10110 < w_i \leq 10^{11}): | Test No. | Scale of nn | Scale of kk | Remarks | | :------: | :-----------: | :----------: | :-----: | | 11 | n=3n=3 | k=2k=2 | | | 22 | n=5n=5 | ^ | ^ | | 33 | n=16n=16 | ^ | All wiw_i are equal | | 44 | n=1000n=1\,000 | ^ | wiw_i are uniformly random within the range | | 55 | ^ | ^ | | | 66 | n=100000n=100\,000 | ^ | ^ | | 77 | ^ | ^ | All wiw_i are equal | | 88 | ^ | ^ | | | 99 | n=7n=7 | k=3k=3 | ^ | | 1010 | n=16n=16 | ^ | All wiw_i are equal | | 1111 | n=1001n=1\,001 | ^ | ^ | | 1212 | n=99999n=99\,999 | k=4k=4 | ^ | | 1313 | n=100000n=100\,000 | ^ | ^ | | 1414 | ^ | ^ | ^ | | 1515 | n=1000n=1\,000 | k=5k=5 | ^ | | 1616 | n=100000n=100\,000 | k=7k=7 | wiw_i are uniformly random within the range | | 1717 | ^ | ^ | | | 1818 | ^ | k=8k=8 | wiw_i are uniformly random within the range | | 1919 | ^ | k=9k=9 | | | 2020 | ^ | ^ | ^ |

Additional Tips

Contestants should use 64-bit integers for input, output, storage, and computation.

Scoring

For each test point:

  • If the first line of the output is correct, you will receive 40%40\% of the score for that test point.
  • If the entire output is correct, you will receive 100%100\% of the score.

Translated by ChatGPT 5