PivotOJ

Glossary Arrangement

시간 제한: 5000ms메모리 제한: 1024MB출처: NWERC 2021BOJ 23773

문제

In Unix based operating systems, one of the most frequently used commands is ls, which displays a list of all the files in a directory in lexicographic order. In the most basic form, ls prints each filename on a separate line, but to improve readability and save screen space, the list is usually split into multiple columns which are displayed side by side.

A friend of yours is writing a book about NWERC and has put you in charge of editing the glossary of relevant terms at the end of each chapter. Each glossary must use the same multi-column layout as ls, so you decided to go for the lazy option: For each word in the glossary, you created an empty file with that name, and simply let ls do the heavy lifting.

Unfortunately, your friend is not satisfied with your layouts and complains that some of them take up too much vertical space. The problem with your approach is that ls always forms columns of equal height, except for the last column, which may be shorter. This sometimes ends up using more lines than would be needed if the column heights could be chosen more freely:

Figure 1: Saving two lines using variable column heights.

Begrudgingly, you decide to write your own improved version of ls, ls--, that also displays the contents of a directory in lexicographic order, but uses variable column heights to always achieve the lowest possible number of lines.

Columns have a fixed width, which is the length of the longest filename within the column, and are separated by a single space. The names in each column must be left-aligned and padded to the column width using spaces. Also, the terminal you are using has a fixed width of ww characters which the table may not exceed.

Given the contents of a directory as a list of filenames, find an optimal column layout that minimizes the number of lines needed to print the entire list.

입력

The input consists of:

  • One line with two integers nn and ww (1n,w50001 \le n,w \le 5\,000), the number of files and the width of the terminal.
  • nn lines, each with one filename ss (1sw1 \le |s| \le w, ss consists of lowercase English letters).

The filenames are distinct and in lexicographic order. The total number of letters is at most 10610^6.

출력

Output an optimal way of listing the given filenames: \begin{itemize}

  • One line with two integers rr and cc, the number of lines and the number of columns used.
  • One line with cc positive integers a1,a2,,aca_1, a_2, \ldots, a_c, the widths of the columns.
  • The table of filenames, subject to the following formatting:
    • There are cc columns, where column ii has width aia_i for each ii, and within each column there are at most rr filenames that are aligned on the left and grouped at the top.
    • The filenames are aligned using spaces, with exactly one space between columns.
    • The total width of the table is at most ww.
    • When reading column by column, the filenames appear in lexicographic order.

Note that unlike in other problems, you strictly need to follow the above formatting rules for whitespace. However, we still allow trailing whitespace at the end of each line, even if this whitespace exceeds the width ww.

예제

예제 1

입력
9 30
algorithm
contest
eindhoven
icpc
nwerc
programming
regional
reykjavik
ru
출력
3 4
9 5 11 2 
algorithm icpc  programming ru
contest   nwerc regional      
eindhoven       reykjavik

예제 2

입력
6 10
aaa
bb
ccccc
ddd
eeeee
fffff
출력
4 2
3 5
aaa ccccc
bb  ddd
    eeeee
    fffff

예제 3

입력
5 15
pppp
ppppp
pq
pqab
xyzff
출력
2 3
5 2 5
pppp  pq pqab
ppppp    xyzff
이 문제는 채점 준비 중입니다. 테스트 데이터가 확보되면 제출이 가능합니다.