#K92842. Assign Books to Shelves
Assign Books to Shelves
Assign Books to Shelves
You are given n books and k shelves. The books are arranged in a given reading order. Your task is to assign each book to a shelf such that:
- If there is more than one book and only one shelf is available, the assignment is impossible. In that case, output
-1
. - Otherwise, assign the books to shelves using a round-robin strategy: the ith book (0-indexed) is assigned to shelf number ((i mod k) + 1).
Note: The input list of book IDs is given but is not used in the assignment logic.
The required assignment method guarantees that if a valid assignment exists, every book is assigned a shelf number between 1 and k, following a cyclic order.
Mathematically, for each book at index i (with 0 ≤ i ≤ n-1), the assigned shelf is given by:
$$ \text{shelf}[i] = (i \bmod k) + 1 $$If k = 1
and n > 1
, the output should be -1
.
inputFormat
The first line of input contains two integers n
and k
separated by a space, where n
is the number of books and k
is the number of shelves.
The second line contains n
space-separated integers representing the book IDs (this list is provided but not used for the assignment).
outputFormat
If a valid assignment is possible, output a single line with n
space-separated integers where the ith integer represents the shelf number assigned to the ith book (0-indexed order).
If the assignment is impossible, output -1
.
5 3
3 1 4 5 2
1 2 3 1 2
</p>