Editorial for WC '18 Contest 3 S1 - The Perfect Team
Submitting an official solution before solving the problem yourself is a bannable offence.
The highest-level Pokémon of each type should certainly be chosen. In addition to those Pokémon, the highest-level Pokémon of the remaining ones should also be chosen to round out the team. We can refer to these additional Pokémon as "extras".
Let's sort all 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 (for each possible ), and the number of extras chosen so far.
When considering the -th Pokémon, if we have not yet chosen a Pokémon of type , 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 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 Pokémon in this manner, we're guaranteed to have successfully chosen the highest-level one of each type as well as the highest-level valid extras, meaning that our aggregated sum of chosen Pokémon levels must be optimal. The time complexity of this solution is , necessary for sorting the Pokémon.
Comments