Ukje and Mysophobia
InterviewTime limit2sMemory limit256 MB
Sort a permutation of 1 to N by repeatedly reversing intervals, using at most N*N reversals to make card i sit at position i.
- Level
Medium6 of 10
- Topics
- Array, Sorting, Implementation, Greedy
- Solved
- No attempts yet
Problem
You are given cards in a row, holding each integer from to exactly once. You may choose any interval with and reverse the whole interval. For instance, reversing in yields .
Number positions from the left. The target arrangement has number on card for every . Apply operations to maximize the number of cards that match the target arrangement. Every permutation can be sorted by reversals alone, so the maximum is always .
Input
The first line gives the number of cards ().
The second line gives the initial arrangement as integers from left to right.
Output
Print the number of operations on the first line. It must satisfy .
On each of the next lines, print the interval used, with and separated by a single space. If is , print nothing more. Applying all operations in order must produce the target arrangement.