Editorial for CPC '19 Contest 1 P2 - Luggage


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Plasmatic

Subtask 1

For this subtask, you can simply brute force all subsets of the items and pick the largest subset with a height range \le K.

Time Complexity: \mathcal O(2^N \cdot N)

Subtask 2

Note that it is always advantageous to pick items with as close of a height as possible. Therefore, if you sort the items the question simply becomes finding the largest subarray with a height range \le K. Brute forcing all subarrays and picking the largest one with a height range \le K is fast enough for the second subtask. Finding the height range of a subarray from indices i to j is simply arr[j]-arr[i] since the array is sorted.

Time Complexity: \mathcal O(N^2)

Subtask 3

To get full points, you have to optimize the brute force from subtask 2 to either binary search or a two-pointer method.

Time Complexity: \mathcal O(N \log N)


Comments

There are no comments at the moment.