Editorial for COCI '11 Contest 4 #3 Keks


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.

After removing K digits from an N-digit number, N-K digits will remain. So we can rephrase the problem statement and say that we must choose N-K digits that form the maximum possible number.

We will use a greedy approach. We find the largest digit that still allows us to add additional digits after it, and place it as the first digit of our solution. We repeat this for the second digit, and so on. If there are multiple choices at any time, it's easy to see that we can choose the leftmost one without harming our best solution.

If we didn't choose the largest possible digit at some point, we would obviously end up with a smaller number instead, which means that our algorithm is correct.


Comments

There are no comments at the moment.