Editorial for WC '16 Contest 3 S1 - Cutting Edge
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's assume the grid is arbitrarily large for now. The optimal solution when involves just trees, one to the right of the top-left cell, and the other below it. When , we can add just more trees to block off the bottom-right cell in the same fashion. For , we'll need to add a diagonal line of additional trees to increase the "width" of one of those barricades from to , and so on. As can be seen, the differences between the subsequent answers for all possible values of (assuming an initial answer of ) are , etc.
So far, this is all independent of the size of the grid. So exactly what effect do the grid's dimensions have? Firstly, the shortest path from the top-left to the bottom-right cell passes through other cells, meaning that if , the answer is -1
, even if every cell on such a path contained a tree, they could all be cut down. Otherwise, if we assume that the grid is wider than it is tall (), then the lengths of our diagonal barricades added at each step will eventually cap out at , yielding a sequence of answer differences .
What remains is computing the sum of the first terms of this sequence to yield the total answer. This can be done trivially in time, but that's too slow to get full marks, so a closed-form expression will be required. We should split the sequence into two portions – the first terms of the sequence, which are increasing in pairs, and the remaining terms, each of which is . Computing the sum of the required terms in the second portion is then trivial. The first portion can once more be split into two separate arithmetic sequences . We can then determine how much of each of those sequences we need (by dividing by , rounded up and down), and compute their sums in standard fashion in time.
Comments