Editorial for DMOPC '22 Contest 3 P4 - Sketchy Cereal
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
The first observation to make is that swapping costs is the exact same as swapping values. Furthermore, we can view each swap as a combination of two actions: ignoring the value of one item (i.e. add only its cost) and ignoring the cost of another item (i.e. add only its value). This leads to the dynamic programming state the maximum total value attainable from the first items with a total cost of , given that we've ignored the value of items and we've ignored the cost of items. The transitions are almost identical to those of 0-1 knapsack, and the final answer is the maximum of all states where .
Time Complexity:
Subtask 2
We will first sort the items in increasing order of cost. Now, we realize that all swaps proceed in one direction. That is, we will always ignore the value of an earlier item in exchange for being able to ignore the cost of an item later on. A very simple solution now follows from this observation: if we define our state to be the maximum total value attainable from the first items with a total cost of given that we've ignored the value of items, then the final answer is the maximum of all the largest values from the last items.
Time Complexity:
Subtask 3
For full marks, we observe that most of the time it is optimal to use all of our swaps. Furthermore, there exists some prefix of length in which we take all of the costs and the largest values among them, and there exists some suffix of length in which we take the largest values among the last items. All of the swapping occurs between the prefix and the suffix, so the optimal choices for the middle portion can be computed simply by using normal 0-1 knapsack.
The only time it is better to use less than swaps is when we have more than enough swaps to take as many of the smallest costs as possible, and to each of these costs assign the largest possible values. This case should be handled carefully in order to obtain full marks.
Time Complexity:
Comments