Editorial for DMOPC '18 Contest 3 P3 - Bob and Math Class
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For the first subtask, check each subarray. You can find the longest block of only Ts or only Fs in a subarray in . There are 
 subarrays, so the final time complexity is 
.
Time Complexity: 
For the second subtask, when considering subarrays, categorize them by their left endpoint. Then you can find the longest block of only Ts or only Fs in each of the subarrays with a certain left endpoint in .
Time Complexity: 
The third subtask was meant to reward any badly done  solutions or potential 
 or 
 solutions. There is no intended solution.
There are several solutions to the final subtask. The solution discussed below is difficult to come up with, but has a simple implementation.
All indices are -indexed. Let 
 be the length of the block of only 
Ts or only Fs which the element at index  is part of. These values can be computed in 
.
Now consider any odd-length subarray. Observe that if this subarray is suspicious, then the middle element must be part of the subarray's longest block of only Ts or Fs. We will count the number of odd-length subarrays given its middle element. From the previous observation, it can be seen that the number of odd-length subarrays with middle element at index  is 
.
The reasoning for even-length subarrays is similar. If the  and 
 answers are both 
T or both F, there are  even-length subarrays whose two middle elements are at index 
 and 
. If the 
 answer differs from the 
 answer, then there are 
 even-length subarrays.
Time Complexity: 
Comments