This page is still under construction.

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

Tiling Up Blocks

Interview

Time limit1sMemory limit128 MB

Summary
Find the largest subset of blocks that stacks so both knob counts never decrease from bottom to top.
Level

Medium5 of 10

Topics
Dynamic programming, Sorting
Solved
No attempts yet

Problem

Michael the Kid got a game set from his grandparent as a birthday gift. The box holds nn tiling blocks, and every block has the shape below.

Figure 1: a tiling block with parameters (3,2)(3, 2).

Every block carries two parameters (ℓ,m)(\ell, m). The upper face has ℓ\ell protruding knobs on the left and mm protruding knobs in the middle, and the bottom face is carved with ℓ\ell dens on the left and mm dens in the middle at the matching spots.

An (ℓ,m)(\ell, m) block clearly sits on another (ℓ,m)(\ell, m) block, and that is not the only legal placement. An (ℓ,m)(\ell, m) block can be tiled upon an (ℓ′,m′)(\ell', m') block if and only if ℓ≥ℓ′\ell \ge \ell' and m≥m′m \ge m'.

You are given a box B={b1,b2,…,bn}B = \{b_1, b_2, \dots, b_n\} of nn blocks, where block bib_i has parameters (ℓi,mi)(\ell_i, m_i). Report how many blocks the tallest tower built from BB contains.

Input

The input lists several game boxes back to back. Each box starts with the integer nn, the number of blocks inside it. The next nn lines each hold two integers, the left parameter ℓi\ell_i and the middle parameter mim_i of the ii-th block.

nn is at most 10 00010\,000, and ℓi\ell_i and mim_i lie between 11 and 100100. A value of n=0n = 0 marks the end of the input.

Output

For each box, print on its own line how many blocks the tallest tower built from that box contains. After the last box, print a single star * on a line of its own.

Examples1

  1. Example 1

    Input
    3
    3 2
    1 1
    2 3
    5
    4 2
    2 4
    3 3
    1 1
    5 5
    0
    
    Expected output
    2
    3
    *