#P14496. [NCPC 2025] Bohemian Bookshelf
[NCPC 2025] Bohemian Bookshelf
Description
When it comes to shelving books, bibliophiles and interior decorators rarely agree: vertical or horizontal---what’s the right way? Ever skeptical of orthodoxy, you embrace both! Your bookshelf displays a composed mix: upright volumes standing with quiet discipline next to a single, neatly arranged stack, like a literary ziggurat. The effect signals bohemian chic with a touch of endearing academic absent-mindedness and just enough polish to suggest you’ve definitely read some of the books.
A book is characterised by its spine height and thickness. It can fit upright if its height does not exceed the bookshelf's height. Alternatively, books can form a stack by being laid flat on their sides on top of each other; for aesthetic reasons the books forming such a stack must be arranged in nonincreasing order of spine height. Their total thickness may not exceed the height of the bookshelf. The total thickness of the upright books and the width of the stack may not exceed the width of the bookshelf.
The books in Sample can be arranged like this:
:::align{center}
:::
Input Format
The input consists of:
- One line with integers , , (, , ), the number of books you want to display, the height of the bookshelf in millimeters, and the width of the bookshelf in millimeters,
- lines, one for each book, with two integers , (, ), the book's spine height and thickness in millimeters. On its own, every book fits on the shelf either standing up or on its side, so we also have .
Output Format
Output two lines.
The first line starts with the word , followed by the indices of the upright books. The second line starts with the word , followed by the indices of the books in the stack, in order from bottom to top. There must be at least one upright book and at least one stacked book. Books are indexed , , .
If there is more than one solution, you may output any one of them. If no such arrangement is possible, print .
3 250 350
178 32
200 60
297 50
upright 1
stacked 3 2
2 300 300
290 60
290 60
impossible
京公网安备 11011102002149号