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.
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 (prefix). Now we want to increase the prefix by adding a new phone. We can place the new phone on the table , because if we put it to the right of table , it will not ring. Obviously, it pays off the most to place the phone on table , 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 . The complexity is .
Comments