Matryoshka Dolls
Time limit1sMemory limit128 MB
Given doll heights, nest each doll inside a strictly taller doll, each holding at most one, to minimize the number of outermost dolls.
- Level
Easy3 of 10
- Topics
- Greedy, Sorting, Two pointers
- Solved
- No attempts yet
Problem
Adam received a box full of Matryoshka dolls (a traditional Russian nesting doll) from his friend Matryona. Every doll has a hollow bottom, so one doll can hold exactly one smaller doll directly inside it.
All the dolls have the same shape but different sizes. Doll is described by a single number , its height. Doll fits directly inside doll if and only if , and each doll may contain at most one doll immediately inside it (that inner doll may in turn hold another doll, forming a nested stack).
Adam wants to nest the dolls so that the number of outermost dolls (dolls that are inside no other doll) is as small as possible. Given the heights of all the dolls, compute this minimum possible number of outermost dolls.
Input
The input consists of multiple test cases. Each test case begins with a line containing an integer (), the number of dolls. Each of the next lines contains one integer (), the height of the -th doll in centimeters. The input ends with a line containing , which must not be processed.
Output
For each test case, print a single line containing the minimum possible number of outermost dolls after nesting the dolls optimally.