아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Glossary Arrangement

시간 제한5초메모리 제한1024 MB

요약
정렬된 파일 이름 목록을 높이가 다른 여러 열로 나누어, 표 전체 너비가 w를 넘지 않으면서 줄 수가 최소가 되도록 배치한다.
난이도

보통10점 중 7점

유형
동적 계획법, 구현
정답자
아직 제출이 없습니다

문제

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 (1≤n,w≤5,0001 \le n,w \le 5\\,000), the number of files and the width of the terminal.
  • nn lines, each with one filename ss (1≤∣s∣≤w1 \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 a_1,a_2,…,a_ca\_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 a_ia\_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.

예제3

  1. 예제 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. 예제 2

    입력
    6 10
    aaa
    bb
    ccccc
    ddd
    eeeee
    fffff
    
    예상 출력
    4 2
    3 5
    aaa ccccc
    bb  ddd
        eeeee
        fffff
    
  3. 예제 3

    입력
    5 15
    pppp
    ppppp
    pq
    pqab
    xyzff
    
    예상 출력
    2 3
    5 2 5
    pppp  pq pqab
    ppppp    xyzff