Editorial for COCI '16 Contest 6 #2 Telefoni


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.

Notice that the series of ringing phones will always be a consecutive series from the one on the first table to the one at the p^\text{th} (prefix). Now we want to increase the prefix by adding a new phone. We can place the new phone on the table p+1, p+2, \dots, p+d, because if we put it to the right of table p+d, it will not ring. Obviously, it pays off the most to place the phone on table p+d, because this way the new prefix will be of maximal length. We implement this by iterating from left to right and keeping track of the location of the last ringing phone, and placing the new phone if the difference between the current position and the table with the last phone is equal to d. The complexity is \mathcal O(N).


Comments

There are no comments at the moment.