Connection Status:
Competition Arena > Pikachu
SRM 533 · 2011-11-22 · by cgy4ever · Dynamic Programming, Greedy, Math, Sorting
Class Name: Pikachu
Return Type: int[]
Method Name: bestEncoding
Arg Types: (vector<int>)
Problem Statement

Problem Statement

Pikachu is a well-known character in the Pokemon anime series. Pikachu can speak, but only 3 syllables: "pi", "ka", and "chu". Therefore Pikachu can only pronounce strings that can be created as a concatenation of one or more syllables he can pronounce. For example, he can pronounce the words "pikapi" and "pikachu".


We need to teach our Pikachu to use N different words, conveniently numbered 0 through N-1. To do this, we need to encode the words into N distinct strings Pikachu will be able to pronounce. For example, we may encode "electric" as "pikapi" and "mouse" as "pikachu". As Pikachu sometimes omits pauses between the words he says, the encoding we will use must be prefix-free. More formally, we need to choose strings code[0], code[1], ..., code[N-1] such that:
  1. For any i, code[i] must be a string that is generated by concatenating one or more copies of the strings "pi", "ka" and "chu".
  2. For any distinct i and j, code[i] is not a prefix of code[j].
Of course, there are infinitely many such codes. We want you to find all the most efficient codes. The N words we need to encode may have distinct frequencies. You are given the relative frequences of all words as a int[] freq. (I.e., the actual frequency of the i-th word is freq[i]/S, where S is the sum of all elements of freq.) For a given code, its total cost can be computed as the sum of (freq[i]*length(code[i])) over all i.


Let A be the minimal total cost that can be achieved, and let B be the number of distinct codes that have the total cost equal to A. Your method must return a int[] with two elements: element 0 should be the value A, and element 1 should be the value (B modulo 1,000,000,009).

Constraints

  • freq will contain between 2 and 30 elements, inclusive.
  • Each element of freq will be between 1 and 1000, inclusive.
Examples
0)
{1,1}
Returns: {4, 2 }

We have two words with the same frequency. One of the two optimal codes is ("pi", "ka"), i.e., word 0 is encoded as "pi" and word 1 is encoded as "ka". The total cost of this code is 1*length("pi") + 1*length("ka") = 1*2 + 1*2 = 4. The other optimal code is ("ka", "pi").

1)
{1,1,2}
Returns: {9, 4 }

There are 4 solutions: ("chu", "ka", "pi"), ("ka", "chu", "pi"), ("pi", "chu", "ka") and ("chu", "pi", "ka"). Note that solutions that encode word 2 as "chu" are not optimal.

2)
{1,1,1,1}
Returns: {13, 48 }

One of the 48 optimal codes is ("pi", "chu", "kapi", "kaka").

3)
{2,3,5,7,11,13,17,19}
Returns: {309, 96 }
4)
{533,533,533,353,335,335}
Returns: {10290, 288 }
5)
{1,1,1,1,1,1,1,1,1,1,1,1,1}
Returns: {72, 362124467 }

Don't forget to use modular arithmetics when computing B.

Submissions are judged against all 107 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class Pikachu with a public method vector<int> bestEncoding(vector<int> freq) · 107 test cases · 2 s / 256 MB per case

Submitting as anonymous