#P1912. [NOI2009] 诗人小G

    ID: 863 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp贪心2009NOI 系列Special Judge

[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 PP-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 TT, the number of poems.

Then follow TT poems, each being one test case. For each test case, the first line contains three positive integers N,L,PN, L, P separated by spaces: NN is the number of sentences in the poem, LL is the standard line length, and PP is as defined above.

Starting from the next line, there are NN lines, each containing one sentence. A sentence consists of symbols such as English letters, digits, and punctuation (ASCII codes from 3333 to 127127, but not including -).

Output Format

For each test case, if the minimal disharmony does not exceed 101810^{18}, 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 101810^{18}, output Too hard to arrange. After each test case, output a line -------------------- consisting of exactly 20 - characters (ASCII code 4545). 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 66; in the last two test cases, the actual length of each line is 44. 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 TT NN LL PP
11 10\le 10 18\le 18 100\le 100 5\le 5
22 ^ 2×103\le 2\times 10^3 6×104\le 6\times 10^4 10\le 10
33 ^ ^
44 5\le 5 105\le 10^5 200\le 200
55 ^ ^
66 3×106\le 3\times 10^6 22
77 ^ ^
88 10\le 10
99 ^
1010

The length of every sentence does not exceed 3030.

Translated by ChatGPT 5