Rectangles
Time limit1sMemory limit128 MB
Find the longest chain of rectangles where each rectangle's upper-right corner is strictly below and left of the next rectangle's lower-left corner.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
A rectangle in the coordinate plane is given by a pair of corner coordinates: its lower-left corner and its upper-right corner , where and .
For two rectangles and , we say that precedes , written , if
In other words, the upper-right corner of must be strictly smaller than the lower-left corner of in both coordinates.
Given a collection of rectangles, find the length of the longest sequence of rectangles such that
Input
The input consists of several test cases. Each test case begins with a line containing a single integer , the number of rectangles. Each of the next lines contains four integers , giving the lower-left and upper-right corners of the -th rectangle, where and .
The end of input is indicated by a line containing a single .
Output
For each test case, print the length of the longest chain as a single integer on its own line.