#K78537. Arrange Books on Shelves

    ID: 35108 Type: Default 1000ms 256MiB

Arrange Books on Shelves

Arrange Books on Shelves

You are given several shelves and a collection of books. Each shelf has a maximum height capacity, and each book has its own height. Your task is to assign every book to the first shelf (with a 1-based index) that can accommodate it, i.e. the shelf's height is greater than or equal to the book's height. If no shelf can accommodate a book, output -1 for that book.

Mathematically, for each book with height ( b ), you need to find the smallest index ( i ) such that ( b \leq s_i ) where ( s_i ) is the height capacity of the ( i^{th} ) shelf. If no such ( i ) exists, the answer for that book is (-1).

inputFormat

Input is provided via standard input (stdin) in the following format:

  1. The first line contains an integer ( n ), representing the number of shelves.
  2. The second line contains ( n ) space-separated integers; each integer denotes the maximum height of a shelf.
  3. The third line contains an integer ( m ), representing the number of books.
  4. The fourth line contains ( m ) space-separated integers; each integer denotes the height of a book.

Note: If ( n = 0 ), the second line will be empty. Similarly, if ( m = 0 ), the fourth line will be empty.

outputFormat

Output a single line to standard output (stdout) containing ( m ) space-separated integers. Each integer is either the 1-indexed shelf number where the corresponding book is placed, or (-1) if the book cannot be placed on any shelf.## sample

4
5 10 15 20
5
4 9 21 2 11
1 2 -1 1 3

</p>