Editorial for Baltic OI '09 P1 - Beetle
Submitting an official solution before solving the problem yourself is a bannable offence.
We will assume that , this can be done in . Here, is an extra added "start" drop we will find useful, and is the total number of drops with this new drop included.
Suppose that the beetle drinks drops at time moments respectively. The total amount of water drunk will be . Since is fixed for fixed , we are to minimize the sum . Actually, this formula only holds if , because the beetle does not really get any "penalty points" for passing a drop that is no more. However, letting these penalty points will not change the maximal possible amount, as there is always an optimal route in which the beetle stops before (and it is clever to do so!).
Also notice that the next drop the beetle will drink is always either the closest from left or from right, because there's no point in not drinking a drop if one is passing it anyway.
From here on we use dynamic programming. Let be the least possible "penalty sum" beetle would get if at time moment it started at and would drink exactly drops, but there were no drops . Similarly, let be the least possible "penalty sum" for drinking drops starting at if there were no drops .
If the beetle willing to drink drops starts at and there are no drops , then it can either go for drop at or , which reduces the problem to either or . The penalty (time spent) for current drop will be either or . In any case, this penalty will count in for all other drops the beetle is going to drink. Therefore the following equations hold:
The boundary conditions are:
Now we can check what is the maximal amount of water the beetle can drink if starts at time moment at and there is no drop :
We find it by consequently calculating and values in time and memory.
Comments