Matryoshka Dolls, Again
Time limit1sMemory limit128 MB
Given N dolls each with three dimensions, nest them so every inner doll is strictly smaller on all three axes; minimize the number of outermost dolls.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, Sorting, Greedy
- Solved
- No attempts yet
Problem
Adam has received yet another box of Matryoshka dolls from Matryona. This time the dolls come in all shapes and sizes. Each doll is described by three numbers: its width , length , and height .
Doll can be placed inside doll if and only if , , and all hold. In other words, every one of the three dimensions must be strictly smaller, and a doll may not be rotated when it is nested. Moreover, each doll can hold at most one other doll directly inside it.
Nest the dolls inside one another so that the number of outermost dolls is as small as possible, and report that minimum number.
Input
The input consists of several test cases. Each test case starts with a line containing a single integer , the number of dolls (). Each of the next lines contains three space-separated integers , , and (), the dimensions of the -th doll.
The input ends with a line containing , which must not be processed.
Output
For each test case, print on its own line the minimum possible number of outermost dolls after nesting the given dolls optimally.