Editorial for TLE '16 Contest 5 P2 - Playing Piano


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

First, note that since each chord spans 12 notes, there are only 12 unique chords to consider. Additionally, since the chords steps cycle every 12 steps, we can take every note modulo 12.

Consider that the super chord starts on note 1. Then use a two-pointer algorithm to find the longest interval containing no more than K notes that are not in the chord starting from note 1. To do this, we keep track of how many non-chord notes there are. When we increment the right pointer, we keep incrementing the left pointer until the number of non-chord notes is not greater than K.

Now, we repeat our solution for the other 11 chords and output the greatest answer found.

Time Complexity: \mathcal{O}(N)


Comments

There are no comments at the moment.