#P1912. [NOI2009] 诗人小G
[NOI2009] 诗人小G
Description
Little G is an excellent poet who often writes poems for fun. However, he has always been troubled by typesetting.
A poem contains several sentences. Some consecutive short sentences can be put on the same line separated by single spaces. Note that there is no limit on how many sentences can be placed on a line. Little G defines a standard line length for each poem (a line’s length is the total number of symbols in that line). He hopes that, after typesetting, the length of every line is close to the standard line length. Clearly, the original order of sentences must not be changed, and Little G does not allow a sentence to be split across lines. Under these two conditions, he defines the disharmony of a line as the -th power of the absolute difference between the line’s actual length and the standard line length. The disharmony of a typesetting is the sum of the disharmonies of all lines.
Little G has recently composed several poems. Please typeset each poem so that the disharmony is minimized, and tell him the result.
Input Format
The first line of the input contains an integer , the number of poems.
Then follow poems, each being one test case. For each test case, the first line contains three positive integers separated by spaces: is the number of sentences in the poem, is the standard line length, and is as defined above.
Starting from the next line, there are lines, each containing one sentence. A sentence consists of symbols such as English letters, digits, and punctuation (ASCII codes from to , but not including -).
Output Format
For each test case, if the minimal disharmony does not exceed , print that disharmony on the first line. Then print the typeset poem on the following lines. Note: between two adjacent sentences on the same line, insert exactly one space.
If there are multiple optimal solutions (i.e., with the same minimal disharmony), you may output any of them. If the minimal disharmony exceeds , output Too hard to arrange. After each test case, output a line -------------------- consisting of exactly 20 - characters (ASCII code ). Do not print extra blank lines or spaces.
4
4 9 3
brysj,
hhrhl.
yqqlm,
gsycl.
4 9 2
brysj,
hhrhl.
yqqlm,
gsycl.
1 1005 6
poet
1 1004 6
poet
108
brysj,
hhrhl.
yqqlm,
gsycl.
--------------------
32
brysj, hhrhl.
yqqlm, gsycl.
--------------------
Too hard to arrange
--------------------
1000000000000000000
poet
--------------------
Hint
Explanation for Sample Input and Output 1
In the first two test cases, the actual length of each line is ; in the last two test cases, the actual length of each line is . In a typesetting scheme, the spaces between two adjacent sentences on the same line are counted in that line’s length (see the second dataset in the sample). There is no trailing space at the end of a line.
Constraints
| Test Point | ||||
|---|---|---|---|---|
| ^ | ||||
| ^ | ^ | |||
| ^ | ^ | |||
| ^ | ^ | |||
| ^ | ||||
The length of every sentence does not exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号