Bulletin boards
Time limit1sMemory limit128 MB
Arrange ordered framed pictures into consecutive strips with shared frames and centering to fit the smallest area rectangle.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Simulation
- Solved
- No attempts yet
Problem
The kindergarten is decorating one of its walls with pictures. The winner of the decoration contest is a series called "History of Mathematics", and now someone has to decide where it goes. That means working out the smallest rectangle by area that the whole series fits into.
Every picture is framed before it goes up. The frame is one character thick on each side, so a picture with rows and columns of content takes up rows and columns on the wall.
The pictures keep the order they are given, read the way text is read: left to right inside a row, then on to the next row. Anička Zrzavá, who teaches art to the computer science students, has her own requirements.
- The wall is divided into horizontal strips. Every picture belongs to one strip and never reaches into another strip, not even with its frame. A strip holds pictures that are consecutive in the given order.
- Inside a strip the pictures sit tight against each other, so two neighbouring frames share exactly one column.
- Each picture is centred vertically inside its strip. When two positions are equally centred, it goes to the higher one.
- The whole run of pictures in a strip is centred horizontally inside the rectangle. When two positions are equally centred, it goes to the left one.
Among all arrangements take the rectangle with the smallest area. If several rectangles reach that area with different shapes, take the widest one. If the pictures can be divided into strips in more than one way inside that rectangle, take the division whose first strip holds as many pictures as possible; if that still leaves a choice, make the second strip hold as many as possible, and so on.
Input
The input consists of several blocks. Every block except the last one starts with a line holding a single number , the number of pictures in the series, where . The last block starts with a line holding zero and is not processed.
A block continues with picture descriptions. Each description starts with a line holding and , the height and the width of the picture without its frame, where . The next lines hold the contents of the picture, again without the frame. Each of those lines holds at most characters, which may be uppercase letters, lowercase letters, digits or spaces. Characters missing from the end of a line count as spaces.
Output
For each block print the height, the width and the area of the smallest and most pleasing arrangement on one line, then draw that arrangement.
Draw the frames with the characters +, - and |, and fill them with the contents from the input, padded with the missing spaces. Where two frames share a column, print + at the corner positions of both frames.
Print the character : at the start and at the end of every line of the drawing. It does not count towards the size of the wall.
Print one empty line between the output of two consecutive blocks.