Nested Dolls
Time limit1sMemory limit128 MB
Given a set of dolls with widths and heights, partition them into the fewest strictly increasing chains in both dimensions, which equals the longest antichain by Dilworth's theorem.
- Level
Medium7 of 10
- Topics
- Sorting, Dynamic programming, Binary search, Greedy
- Solved
- No attempts yet
Problem
Dilworth is the world's most prominent collector of Russian nested dolls (matryoshkas): he owns thousands of them. These are the wooden, hollow dolls of different sizes, where the smallest doll sits inside the second-smallest, which in turn sits inside the next one, and so on.
One day he wonders whether there is a different way to nest them that leaves him with fewer separate nested dolls, which would make his collection even more magnificent.
He unpacks every doll and measures the width and height of each one. A doll with width and height fits inside another doll with width and height if and only if and . Each doll can directly contain at most one other doll (which may itself contain another), so the dolls in a single nested set form a chain that is strictly increasing in both width and height.
Given all the measurements, compute the smallest number of separate nested dolls (sets) into which the entire collection can be assembled.
Input
The first line contains a single integer (), the number of test cases.
Each test case begins with a line containing a single integer (), the number of dolls. It is followed by integers , where is the width and is the height of doll (). These integers may be split across one or more lines and are separated by whitespace.
Output
For each test case, output a single line containing the minimum number of separate nested dolls (sets) needed to hold the entire collection.