First, sort all type numbers lexicographically by to obtain a permutation . The growth-coefficient order of any selected sample is a subsequence of this permutation.
Fix a day , and place the pair at each position of the permutation. Compare pairs by their first components, breaking ties by their second components. The selected types have the same population order exactly when their pairs are strictly increasing under this comparison. Thus, the maximum number of types that can be selected on day is the length of the longest increasing subsequence of these pairs. Comparing only populations would miss the rule for tied populations, so type numbers must also be included.
Compute the longest increasing subsequence by maintaining the smallest possible final pair for every subsequence length. Process the pairs in order. Use binary search to find the first stored final pair greater than or equal to the current pair, and replace it with the current pair. If no such position exists, append the current pair. This check takes time.
A sample that satisfies the condition on a certain day also satisfies it on every later day. Consequently, the predicate that the longest increasing subsequence has length at least changes only from false to true as the day increases. Let . If the condition holds on day , output immediately. Otherwise, if the length is still less than on day , output . In the remaining cases, binary search over for the first day satisfying the condition.
The initial sort takes time, and there are checks. The total time complexity is , and the space complexity is .
Proof
Consider two types in their growth-coefficient order, with appearing first. Then . The population difference never decreases as increases.
Solution written by GPT6
-1If also precedes in population order on day , then either , or and . The difference can only increase or stay equal on a later day, so continues to precede in both cases. Therefore, a valid sample remains valid on every later day, proving monotonicity.
If , then on day the difference satisfies . Hence, two types with different growth coefficients necessarily have consistent orders on day . If their growth coefficients are equal, their population difference never changes, so whether their orders agree never changes either. Thus, any sample that is valid on any day is also valid on day . If no sufficiently large sample exists on day , none can exist on any day. Otherwise, the earliest valid day lies in .
Listing the selected types in growth-coefficient order gives a subsequence of . Their population order is identical exactly when the corresponding pairs are strictly increasing. Valid samples therefore correspond exactly to increasing subsequences.
Among subsequences of the same length, keeping the smallest final pair cannot reduce the possible extensions by later pairs. Replacing the first stored final pair greater than or equal to the current pair extends a shorter subsequence to the current length; no longer subsequence can end at the current pair. Maintaining this invariant makes the length of the stored array exactly the longest increasing subsequence length. The check is therefore correct, and binary search with this correct monotone predicate returns the earliest valid day.