Editorial for WC '18 Contest 3 S1 - The Perfect Team


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.

The highest-level Pokémon of each type 1 \dots K should certainly be chosen. In addition to those K Pokémon, the M-K highest-level Pokémon of the remaining N-K ones should also be chosen to round out the team. We can refer to these additional M-K Pokémon as "extras".

Let's sort all N Pokémon in non-increasing order of level, such that the highest-level ones come first, and then iterate over them in that order while greedily determining which ones to choose. Along the way, we'll maintain several pieces of information: the level sum of Pokémon chosen so far, an array indicating whether or not we've already chosen a Pokémon of type t (for each possible t), and the number of extras chosen so far.

When considering the i-th Pokémon, if we have not yet chosen a Pokémon of type T_i, then we should go ahead and choose this one, as it must be the highest-level Pokémon of its type. Otherwise, if we have not yet chosen M-K extras, then we should go ahead and choose this one as an extra, as it must be the highest-level valid extra.

Upon processing all N Pokémon in this manner, we're guaranteed to have successfully chosen the highest-level one of each type as well as the M-K highest-level valid extras, meaning that our aggregated sum of chosen Pokémon levels must be optimal. The time complexity of this solution is \mathcal O(N \log N), necessary for sorting the Pokémon.


Comments

There are no comments at the moment.