#K4021. Arrange Books on Shelves
Arrange Books on Shelves
Arrange Books on Shelves
You are given n shelves and m books. Each shelf has a maximum width capacity given by an array \(w\) of length n and can hold at most k books. The m books have widths specified in the array book_width.
Your task is to determine if it is possible to arrange all the books on the shelves such that:
- No shelf holds more than k books.
- The total width of the books on a shelf does not exceed the shelf's maximum width \(w_i\) for the i-th shelf.
If an arrangement exists, output one arrangement by printing the indices of the books (1-indexed) placed on each shelf. If there is no valid arrangement, output -1.
Note: You may output any valid arrangement that satisfies the constraints.
inputFormat
The input is given from standard input (stdin
) in the following format:
n m k w[1] w[2] ... w[n] book_width[1] book_width[2] ... book_width[m]
Where:
- n is the number of shelves.
- m is the number of books.
- k is the maximum number of books allowed on any shelf.
- The second line contains n integers representing the maximum width for each shelf.
- The third line contains m integers representing the width of each book.
outputFormat
If no valid arrangement exists, print -1.
Otherwise, print exactly n lines. The i-th line should contain the indices of the books placed on the i-th shelf separated by spaces. If a shelf remains empty, print an empty line for that shelf.
All output should be written to standard output (stdout
).
3 5 3
10 10 10
2 3 4 5 1
5 1 2
3 4
</p>