#K65157. Minimum Shelves Required
Minimum Shelves Required
Minimum Shelves Required
You are given N books and a shelf with a maximum capacity C. Each book i occupies a certain space given by a_i. Your task is to compute the minimum number of shelves required to store all the books if you can place books on a shelf as long as the total space occupied does not exceed the shelf capacity C.
Note: You are allowed to arrange the books in any order. One possible strategy is to sort the books in descending order and then use a greedy algorithm to allocate books to shelves. Mathematically, you are to minimize the number of shelves S such that:
\[ \sum_{i \in S_j} a_i \leq C,\quad \text{for every shelf } S_j,\ j=1,2,\ldots,S \]
It is guaranteed that each book's size is a positive integer and that a valid arrangement always exists.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains two space-separated integers: N (the number of books) and C (the capacity of each shelf).
- The second line contains N space-separated integers:
a1, a2, ..., aN
, whereai
is the space taken by the i-th book.
outputFormat
Output a single integer to stdout
— the minimum number of shelves required to store all the books.
5 10
2 3 8 9 3
3
</p>