Boxes on a Shelf
Time limit2sMemory limit512 MB
Choose which boxes to place on a shelf of length L so that each box's center lies strictly inside the shelf, maximizing the count.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Math, Two pointers
- Solved
- No attempts yet
Problem
Varya has a barn where she stores boxes with important stuff. She recently nailed another shelf to the barn wall. Now she wants to put as many boxes as possible on the new shelf.
We shall look at the wall with the shelf as a plane. The shelf is then a horizontal segment of length . There is empty space to the left and to the right of the shelf, which we can consider to be infinite. Box number is a rectangle with sides and .
A box can stand on the shelf and not fall when one of its sides, or a part of it, lies on the shelf, and the center of the box is above an interior point of the shelf. Boxes can be placed only on the shelf, close to the wall: they can not be placed on top of other boxes or in several layers.
Find is the maximum number of boxes Varya can put on the shelf.
Input
The first line contains an integer : the number of boxes (). Each of the following lines contains two integers and : the sides of the respective box (). The last line contains an integer : the length of the shelf ().
Output
Print one integer: the maximum number of boxes Varya can put on the shelf so that they don't fall off.
Notes
In the first example, Varya can place three boxes on the shelf: for example, the second box lying on side of length on the left, the fourth box lying on side of length at the center, and the first box lying on side on the right.
In the second example, if all three boxes are placed on the shelf symmetrically, they will fall off, as the centers of left and right boxes will be located exactly above the ends of the shelf.