Tiling Up Blocks
InterviewTime limit1sMemory limit128 MB
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 tiling blocks, and every block has the shape below.

Figure 1: a tiling block with parameters .
Every block carries two parameters . The upper face has protruding knobs on the left and protruding knobs in the middle, and the bottom face is carved with dens on the left and dens in the middle at the matching spots.
An block clearly sits on another block, and that is not the only legal placement. An block can be tiled upon an block if and only if and .
You are given a box of blocks, where block has parameters . Report how many blocks the tallest tower built from contains.
Input
The input lists several game boxes back to back. Each box starts with the integer , the number of blocks inside it. The next lines each hold two integers, the left parameter and the middle parameter of the -th block.
is at most , and and lie between and . A value of 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.