Editorial
Let be the number of fixed points in the initial permutation.
Suppose we reverse the segment and set . The value originally at position inside the segment moves to position . Hence it becomes a fixed point after the reversal if and only if , or equivalently .
Thus the number of fixed points after reversing is
We need to maximize the difference between the subtracted term and the added term.
For a fixed value , the possible range of is
For a position satisfying , the condition that lies inside is equivalent to . For every , store all values with and sort them. Then the number of newly created fixed points for a given can be found by binary search.
The number of old fixed points in a segment is obtained with a prefix sum. For a fixed , as increases, the number of newly created fixed points changes only after passing one of the stored values, while the number of old fixed points can only decrease. Therefore it is enough to test the smallest possible , and values of the form for stored values .
Each position belongs to exactly one group , so the total number of tested candidates is . The total time complexity is including sorting and binary searches, and the memory complexity is .
Solution written by GPT5.5