Editorial for DMPG '18 G2 - Gardening Fun
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
The key idea is that when watering a subarray, do not consider the entire subarray. Instead, consider the number of times a certain plant is watered as a result of these subarrays. Let be the minimum cost to water the first plants and such that the plant has been watered by subarrays. Then we add for each new subarray at this step and for each subarray at this step. So we obtain the following recurrence:
Call the largest value of . It is not hard to see that we only need to consider up to . The constraints for the first subtask allow this recurrence to pass in time.
Time Complexity:
For the second subtask, we will perform an optimization so that the transition becomes instead of the original . Rewrite the recurrence by rearranging the terms which do not depend on :
We can precompute and in for all and for each by doing a running minimum. So we can now do the transition in .
Time Complexity:
Comments