Editorial for COCI '10 Contest 5 #5 Dvoniz
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us freeze the middle of an interesting sequence in all possible ways. It can easily be noticed that if there exists a sequence of length , there also exists a sequence of length with the same middle value for each integer less than . This suggests a binary search approach to be used to find the longest interesting sequence for every position of the middle element, which yields an algorithm with time complexity.
For each element, we first search for the longest sequence which starts with that element. To solve that problem, we process elements from right to left. Among all interesting sequences which start before the position of the current element we are processing we search for the one for which the value is maximal. The value of can be found as follows: as we process the elements, we keep track of the maximum and we try to improve that maximum by considering interesting sequences which start at the current position.
When we move to the right, we decrease that maximum by (since a move decreases this value by ).
The above algorithm can easily be implemented with time complexity, which solves the problem.
Comments