Editorial for UTS Open '24 P1 - Watering the Plants
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Since , Marcus' watering can only holds one unit of water. Thus, he must walk back to position to refill his watering can every time he waters a flower. The distance Marcus needs to travel to water flower and return to position is . So, the minimum total distance Marcus needs to travel to water all the flowers is:
In python terms, the answer is 2*sum(range(1, N+1))
Since is quite small, this can either be calculated with a loop, or with the simplified formula.
Time Complexity: or
Subtask 2
Since might be greater than , we must consider how to water multiple plants in one trip to minimize the walking distance. The critical observation to solve this problem is to realize that in any given moment, the furthest flowers should always be watered together, since that reduces the maximum distance that Marcus has to walk in a future trip the most. Thus, we can group the flowers into blocks of starting from the rightmost flower, and perform trips forward and back, similarly to Subtask 1. Note that the leftmost block of flowers may contain less than flowers.
For example, if there are flowers and Marcus' watering can holds units of water, he should first water plants and , then plants and , and finally plant . In total, he will walk units of distance.
In python terms, the answer is 2*sum(range(N, 0, -K))
Since is quite small, this can be calculated with a loop.
Time Complexity:
Bonus
Python golf solution (49 bytes): https://dmoj.ca/src/6848690
It is possible to solve this question in time.
In total, there will be blocks of flowers. The distance Marcus needs to travel to water the th block from the right and return to position is . Thus, the minimum total distance is:
Tip: you can perform ceiling division of positive integers using floor division!
Time Complexity:
Comments