You are given a permutation .
You must color every position with one of three colors: red, green, or blue.
A coloring is called valid if the following condition holds for every subarray of length at least :
The score of a valid coloring is the number of subarrays of length at least in which the positions containing the three largest values have pairwise different colors.
Find the maximum possible score of a valid coloring.
There is no subarray of length at least , so every valid coloring has score .
Coloring positions red, green, blue, red is valid. The subarrays and contribute to its score.
In every valid coloring, positions have pairwise different colors. Position must differ from position , so at least one of and cannot contribute. Therefore, a score greater than is impossible.
Color the positions red, green, blue, red, green, in this order. This coloring is valid, and every subarray of length at least contributes to its score. There are such subarrays, so no larger score is possible.