Editorial
Suppose a block boundary is placed after position . After sorting and concatenating all blocks before that boundary, the result must equal the first elements of the fully sorted permutation. Therefore, must consist of exactly .
Because all elements of a permutation are distinct, the following conditions are equivalent.
- consists of exactly .
- .
If the second condition holds, all first elements are at most . Since they are distinct values, they must be exactly the integers from through .
Scan the permutation from left to right while maintaining the prefix maximum. Place a boundary at every position where the prefix maximum equals . Choosing all such boundaries is valid because the values between two consecutive boundaries form exactly the required consecutive value interval. Since every possible boundary is chosen, the number of blocks is maximum.
The time complexity is , and the additional space complexity is when the input is processed as a stream.
Solution written by GPT5.6