NOI '09 P2 - Little G the Poet

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 2.5s
Memory limit: 256M

Problem type
National Olympiad in Informatics, China, 2009

Little G is an outstanding poet who frequently writes poetry for his own pleasure and entertainment. However, one issue has constantly been troubling him, and that is the issue of typesetting his poems.

A poem contains many sentences. For some consecutive short sentences, he can separate them using spaces and place them onto one line. Note that there is no limit to the number of sentences that he can place on one line. Little G defined for each poem a standard line length (the length of a line is the total number of characters on the line), and wishes for each line's length after typesetting to not be far from its standard line length. Clearly when typesetting, one should not change the order of the original sentences, and little G does not allow a line to be split up between two or more lines. Satisfying these two conditions, little G would like to define for each line a level of disproportion, being the absolute value of the difference between the actual length of the line and its standard line length, raised to the P-th power. The overall level of disproportion for a typeset is the sum of the levels of disproportion for all of its lines.

Little G just wrote some more poems, and now invites you to typeset them, making their line lengths as proportionate as possible (i.e. minimizing the level of disproportion), finally providing him with the result.

Input Specification

Each test case contains multiple datasets. The first line of input contains the integer T, representing the number of datasets. There will be T datasets to follow.

Each dataset will describe a poem. The first line of each dataset contains three space-separated integers N, L, and P. Here, N is the total number of sentences in the poem, L is the standard length for the poem's lines, and P is the power used to calculate the level of disproportion. In the following N lines of the dataset, each line contains one sentence made up of alphabetical letters, numbers, punctuation, and other characters (ASCII 33~127, but excluding -).

Output Specification

For each dataset, if the minimum level of disproportion doesn't exceed 10^{18}, then you should output a single integer on one line representing the level of disproportion, followed by some number of lines containing the poem itself.

Note: Adjacent sentences on the same line must be separated by a single space.

If there are multiple different typesets that all produce the minimum level of disproportion, you may output any one of them. Otherwise, if the minimum level of disproportion exceeds 10^{18}, then output Too hard to arrange. After the output of each dataset, output a line containing --------------------, a total of 20 - characters (ASCII 45). Please avoid printing excess lines or spaces.

Sample Input

4
4 9 3
brysj,
hhrhl.
yqqlm,
gsycl.
4 9 2
brysj,
hhrhl.
yqqlm,
gsycl.
1 1005 6
poet
1 1004 6
poet

Sample Output

108
brysj,
hhrhl.
yqqlm,
gsycl.
--------------------
32
brysj, hhrhl.
yqqlm, gsycl.
--------------------
Too hard to arrange
--------------------
1000000000000000000
poet
--------------------

Explanation

The first two datasets in the sample input each have standard line lengths of 6. The last two datasets each have standard line lengths of 4. The space that separates adjacent sentences is counted towards the total length of the line (refer to the second dataset in the sample). There are no trailing spaces in any line.

Scoring

For each test case, if your program produces the correct minimal level of disproportion for all of the datasets, then you will score 70\% of points for the test case. Under this circumstance, if the typesetting scheme for all of the datasets in the test case matches the levels of disproportion that you have outputted, then you will score the remaining 30\% of points. Note: formatting errors in your output can result in no points for the test case.

Constraints

There are 10 total test cases, their constraints satisfy:

Test CaseTNLP
1\le 10\le 18\le 100\le 5
2\le 10\le 2\,000\le 60\,000\le 10
3\le 10\le 2\,000\le 60\,000\le 10
4\le 5\le 100\,000\le 200\le 10
5\le 5\le 100\,000\le 200\le 10
6\le 5\le 100\,000\le 3\,000\,0002
7\le 5\le 100\,000\le 3\,000\,0002
8\le 5\le 100\,000\le 3\,000\,000\le 10
9\le 5\le 100\,000\le 3\,000\,000\le 10
10\le 5\le 100\,000\le 3\,000\,000\le 10

In all of the test cases, the length of sentences will never exceed 30.

Problem translated to English by Alex.


Comments

There are no comments at the moment.