Editorial for CCC '16 S4 - Combining Riceballs


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: FatalEagle

Suppose the riceballs are numbered from 1 to N, with riceball i having size A[i]. Define a boolean function f(i,j) to return 1 if the riceballs in the range [i,j] can all be combined into a single riceball, or 0 otherwise. The final answer to the problem is:

max1ijNf(i,j)k=ijA[k]

Remember that no matter how you combine riceballs in an interval, after they are combined the size will always be the same.

We have the following recurrence:

f(i,j)={1if ijmaxiabjf(i,a)f(a+1,b1)f(b,j)otherwise, where k=iaA[k]==bjA[]

To speed this up, notice that for every a there is at most one b that satisfies the size restriction, and when a increases, b decreases, so all valid pairs may be found with a two-pointers algorithm.

Complexity: O(N3)


Comments

There are no comments at the moment.