#C12074. Minimum Number of Shelves Required

    ID: 41461 Type: Default 1000ms 256MiB

Minimum Number of Shelves Required

Minimum Number of Shelves Required

You are given a collection of books and a collection of shelves. Each book has a specific height and each shelf has a maximum height capacity. The books must be placed on the shelves in such a way that the total height of the books on any shelf does not exceed its height limit. A book cannot be split between shelves.

Your task is to determine the minimum number of shelves required to place all the books. Formally, given a shelf height limit \(H\) and a list of book heights \(h_1, h_2, \dots, h_N\), you must partition the books into subsets such that for each subset \(S\), \(\sum_{i \in S} h_i \le H\), and the number of subsets is minimized.

Note: Although the problem is known to be NP-hard in general (it is equivalent to the bin packing problem), you are asked to implement the greedy strategy described below, which sorts the books in descending order and then places each book into the first shelf that can accommodate it. This approach is sufficient for the given input constraints.

inputFormat

The first line of input contains an integer \(T\) (the number of test cases). For each test case, the input is formatted as follows:

  • An integer \(N\), the number of books.
  • A line with \(N\) space-separated integers representing the heights of the books.
  • An integer \(H\), the height limit of each shelf.

All input is read from standard input (stdin).

outputFormat

For each test case, output a single integer in a new line which represents the minimum number of shelves required to accommodate all books under the given shelf height limit. All output should be written to standard output (stdout).

## sample
2
4
1 2 3 4
5
5
5 3 8 2 6
10
2

3

</p>