This page is still under construction.

Parts of this page are still being built. What you see may change.

Rectangles

Time limit1sMemory limit128 MB

Summary
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 (x1,y1)(x_1, y_1) and its upper-right corner (x2,y2)(x_2, y_2), where x1≤x2x_1 \le x_2 and y1≤y2y_1 \le y_2.

For two rectangles A=((x1A,y1A),(x2A,y2A))A = ((x_1^A, y_1^A), (x_2^A, y_2^A)) and B=((x1B,y1B),(x2B,y2B))B = ((x_1^B, y_1^B), (x_2^B, y_2^B)), we say that AA precedes BB, written A⪯BA \preceq B, if

x2A<x1Bandy2A<y1B.x_2^A < x_1^B \quad \text{and} \quad y_2^A < y_1^B.

In other words, the upper-right corner of AA must be strictly smaller than the lower-left corner of BB in both coordinates.

Given a collection of rectangles, find the length LL of the longest sequence of rectangles (A1,A2,…,AL)(A_1, A_2, \dots, A_L) such that

A1⪯A2⪯⋯⪯AL.A_1 \preceq A_2 \preceq \dots \preceq A_L.

Input

The input consists of several test cases. Each test case begins with a line containing a single integer nn (1≤n≤1000)(1 \le n \le 1000), the number of rectangles. Each of the next nn lines contains four integers x1i y1i x2i y2ix_1^i\ y_1^i\ x_2^i\ y_2^i, giving the lower-left and upper-right corners of the ii-th rectangle, where −1000000≤x1i≤x2i≤1000000-1000000 \le x_1^i \le x_2^i \le 1000000 and −1000000≤y1i≤y2i≤1000000-1000000 \le y_1^i \le y_2^i \le 1000000.

The end of input is indicated by a line containing a single 00.

Output

For each test case, print the length of the longest chain as a single integer on its own line.

Examples5

  1. Example 1

    Input
    3
    1 5 2 8
    3 -1 5 4
    10 10 20 20
    2
    2 1 4 5
    6 5 8 10
    0
    
    Expected output
    2
    1
    
  2. Example 2

    Input
    1
    0 0 1 1
    0
    
    Expected output
    1
    
  3. Example 3

    Input
    3
    0 0 1 1
    2 2 3 3
    4 4 5 5
    0
    
    Expected output
    3
    
  4. Example 4

    Input
    4
    0 0 10 10
    0 0 10 10
    0 0 10 10
    0 0 10 10
    0
    
    Expected output
    1
    
  5. Example 5

    Input
    3
    -1000000 -1000000 -999999 -999999
    -500 -500 -400 -400
    0 0 1000000 1000000
    0
    
    Expected output
    3