Editorial for LCC '24 Contest 1 J3 - Trick-or-Treat Planning


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.

Author: AndyMan68

Subtask 1

For this subtask, since Bob's house is always house number 1, and so Bob's path can simply be walking towards the highest numbered house and passing through all other necessary houses along the way.

Time complexity: O(M)

Subtask 2

For this subtask, Bob's path may no longer just be one straight path. Instead, it can be observed that at some point Bob must visit the house with the highest house number and lowest house number. Notice that directly going to the highest and then the lowest house numbers (or lowest then highest) passes through all the other necessary houses along the way, and so this can be shown to be the only two potential optimal paths. In order to ensure the shortest path of the two is chosen, simply head to the house (either the highest or lowest house number) closest to Bob's before heading to the other.

Time complexity: O(M)


Comments

There are no comments at the moment.