The Splitting Club
InterviewTime limit1sMemory limit128 MB
Given distinct ages with member counts and a ratio R, partition all age groups into the fewest contiguous sections where the max count in a section is at most R times the min count.
- Level
Medium6 of 10
- Topics
- Greedy, Two pointers, Sorting, Intervals
- Solved
- No attempts yet
Problem
The ACM (All Can Meet) club was founded to bring together people of all ages so that they could sit together, share their life experiences, and benefit one another. The club became so popular that gathering every member in one place at one time grew practically impossible, so the club decided to split its members into smaller sections. To keep the sections balanced, the director imposed three requirements:
- All members of the same age must belong to the same section.
- Every member must belong to exactly one section.
- Within each section, the largest number of members sharing a single age must be at most times the smallest number of members sharing a single age. This value , called the splitting factor, is a rational number with .
The third requirement prevents a section from containing an age group so much smaller than the others that its members feel out of place.
For example, write [n, m] for a group of members who are years old. In the section {[10, 50], [6, 45], [70, 12], [43, 23]} the largest age group has 70 members and the smallest has 6, so with this section violates requirement 3 because . However, it can be split into the two sections {[10, 50], [6, 45]} and {[70, 12], [43, 23]}, each of which satisfies all three requirements.
Given the splitting factor and the list of members, find the minimum possible number of sections.
Input
The input contains several test cases. The first line of each test case contains an integer and a rational number , where is the number of distinct ages in the club () and is the splitting factor (). Each of the next lines contains two integers and , meaning that the club has members who are years old ( and ); every age is distinct. The input ends with a line containing and , which must not be processed.
The input values are chosen so that any rounding error in the internal binary representation of will not affect the answer.
Output
For each test case, print a single line containing the minimum number of sections that satisfy all three requirements.