Editorial for COCI '17 Contest 2 #3 Doktor


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.

Let the value of the contiguous subsequence (from now on, just subsequence) be equal to the difference between the number of fixed points created by rotating that subsequence and the number of fixed points that disappear by rotating that subsequence. It is clear that we want to choose the subsequence of the maximal possible value.

We can see that a card with the number x that is not a fixed point becomes a fixed point by rotating a subsequence only if the center of the subsequence (the middle member if the subsequence is of odd length, and the point between two middle members if the subsequence is of even length) is equidistant to that card and the card in the x^\text{th} index or, in other words, the position where the card with the number x should be. If the previous condition is met, a card will become a fixed point if and only if it is contained in the subsequence.

Using the observations above, we can try to design a solution: for each possible center of the subsequence, iterate over all subsequences with that center from the smallest to the largest member. We will always spread by exactly 2 cards, so in each step, we can check in constant time if the value of the subsequence has changed, and for how much. This solution calculates the values of all subsequences in the asymptotic complexity of \mathcal O(N^2).

In order to improve the previous solution, we will take advantage of the fact that we are searching for a subsequence with the maximal value, and not the values of all subsequences. In fact, the value of the subsequence when we iterate over all subsequences with the same center in the previous solution only increases at positions where a new fixed point is created. Therefore, it is sufficient to observe, for each center, only the subsequences with card x at the first card, and end at position x, or vice versa.

The previous passage motivates the following solution: for each card, determine the center that can make it a fixed point, and store it in a list of cards for that center. Then, for each center, we sort the associated list by the distance between the card and the center, and iterate over the list and increase the number of newly created fixed points by 1 for each new element of the list.

In order to calculate the value of each of the given subsequences, we also need to find out the number of fixed points that disappear by reversing a subsequence. We can do this in constant complexity if we previously calculate the number of fixed points in all prefixes of the sequence of cards. The asymptotic complexity of the solution by parts is \mathcal O(N) to calculate the fixed points in all prefixes, \mathcal O(N) for associating the cards to the centers, \mathcal O(N \log N) in the worst case for sorting the lists of all the centers, and amortized \mathcal O(N) for iterating over all considered subsequences, because the total number of elements in all the lists is equal to the number of cards, which is N. The total asymptotic complexity is therefore \mathcal O(N \log N), which is sufficient to get all points.

Additionally, it is not too difficult to solve the task in \mathcal O(N), but this is left as an exercise to the reader.


Comments

There are no comments at the moment.