Editorial for LCC '23 Contest 1 S1 - Weakest Warriors
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Firstly, ignore all of the lore - that's just meant to throw you off. Let's rephrase the important part of the question: We want to sort the candies by their fighting value from lowest to highest and break ties by taking ones with lower indices first. Then, we should print the first of those sorted candies in increasing order of their indices.
The first part of this problem can be done in many ways. One example is by sorting these in an array of pairs, where the first number represents the fighting value and the second number represents the index, then using a custom comparator function to prioritize smaller indices in the case of ties.
Another interesting thing that you can do is store the index of each candy as its negative. This way, when the pairs are sorted, by default, the second number will be higher if its original index was lower.
Credits to
for finding that shortcut.C++ Implementation
// where .first represents the fighting value and .second represents the index
vector<pair<int, int>> candies;
sort(candies.begin(), candies.end(), [](pair<int, int> a, pair<int, int> b){
});
Python Implementation
candies = [
(*fighting value*, *candy index*),
(*fighting value*, *candy index*),
(*fighting value*, *candy index*),
...
(*fighting value*, *candy index*)
]
sorted(candies, cmp=lambda x, y: (candy[0], -candy[1]))
Java Implementation
// in candies, .getFirst() is the fighting value and .getSecond() is the index
List<Pair<Integer, Integer>> candies = new ArrayList<>();
Collections.sort(candies, (a, b) -> {
if (a.getFirst() == b.getFirst()) return a.getSecond() > b.getSecond();
return a.getFirst() < b.getFirst();
});
Then, the second part of the problem just requires a simple sorting of integers in increasing order.
Comments