#K93697. Max Books on Shelves
Max Books on Shelves
Max Books on Shelves
You are given n books and m shelves. Each book is represented by its width and height, and each shelf is represented by its maximum width and height. A book can be placed on a shelf if:
- The book's width does not exceed the shelf's width.
- The book's height does not exceed the shelf's height.
- The shelf has enough remaining width to accommodate the book (i.e., the sum of the widths of the books already placed on the shelf plus the new book's width does not exceed the shelf's width).
Your goal is to compute the maximum number of books that can be placed on the shelves under these constraints.
Mathematically, for a book with width \(w_b\) and height \(h_b\) and a shelf with width \(w_s\) and height \(h_s\), the book can be placed on the shelf if and only if:
\[ w_b \leq w_s,\quad h_b \leq h_s \]and if the remaining available width on the shelf \(r_s\) satisfies \(r_s \geq w_b\).
Design an algorithm that assigns books to shelves (each book can only be used once) and outputs the maximum number of books that can be placed on some shelf.
inputFormat
The input is given from standard input (stdin) in the following format:
<n> <m> <book_1_width> <book_1_height> ... <book_n_width> <book_n_height> <shelf_1_width> <shelf_1_height> ... <shelf_m_width> <shelf_m_height>
Where:
- n is the number of books.
- m is the number of shelves.
- Each of the next n lines contains two integers indicating the width and height of a book.
- Each of the following m lines contains two integers indicating the width and height of a shelf.
outputFormat
Output a single integer, which is the maximum number of books that can be placed on the shelves, printed to standard output (stdout).
## sample5 3
3 4
2 2
5 6
4 4
3 2
6 5
5 7
4 4
4