| Time Limit: 2000MS | Memory Limit: 32768K | |
| Total Submissions: 3753 | Accepted: 1889 |
Description
|
|
There are 3 or 4 letters assigned to each key. If you want the first letter of any group, you press that key once. If you want the second letter, you have to press the key twice. For other letters, the key must be pressed three or four times. The authors of the keyboard did not try to optimise the layout for minimal number of keystrokes. Instead, they preferred the even distribution of letters among the keys. Unfortunately, some letters are more frequent than others. Some of these frequent letters are placed on the third or even fourth place on the standard keyboard. For example, S is a very common letter in an English alphabet, and we need four keystrokes to type it. If the assignment of characters was like in the right picture, the keyboard would be much more comfortable for typing average English texts.
ACM have decided to put an optimised version of the keyboard on its new cellular phone. Now they need a computer program that will find an optimal layout for the given letter frequency. We need to preserve alphabetical order of letters, because the user would be confused if the letters were mixed. But we can assign any number of letters to a single key.
Input
After those two lines, there are exactly L lines each containing exactly one positive integer F1, F2, ... FL. These numbers determine the frequency of every letter, starting with the first one and continuing with the others sequentially. The higher number means the more common letter. No frequency will be higher than 100000.
Output
If there are more solutions with the same price, we will try to maximise the number of letters assigned to the last key, then to the one before the last one etc.
More formally, you are to find a sequence P1, P2, ... P L representing the position of every letter on a particular key. The sequence must meet following conditions:
P1 = 1
for each i>1, either Pi = P i-1+1 or Pi = 1
there are at most K numbers Pi such that Pi = 1
the sum of products SP = F1*P1+F2*P2+...+F L*P L is minimal
for any other sequence Q meeting these criteria and with the same sum SQ = SP, there exists such M, 1 <= M <= L that for any J, M < J <= L, P J = Q J, and P M > Q M.
The output for every test case must start with a single line saying Keypad #I:, where I is a sequential order of the test case, starting with 1. Then there must be exactly K lines, each representing one letter, in the same order that was used in input. Each line must contain the character representing the key, a colon, one space and a list of letters assigned to that particular key. Letters are not separated from each other.
Print one blank line after each test case, including the last one.
Sample Input
1 8 26 23456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ 3371 589 1575 1614 6212 971 773 1904 2989 123 209 1588 1513 2996 3269 1080 121 2726 3083 4368 1334 518 752 427 733 871
Sample Output
Keypad #1: 2: ABCD 3: EFG 4: HIJK 5: LM 6: NOPQ 7: RS 8: TUV 9: WXYZ
Source
5498



被折叠的 条评论
为什么被折叠?



