ReverseSort
InterviewTime limit5sMemory limit512 MB
Given a permutation of 1 to N, find the minimum number of subarray reversals needed to sort it into ascending order.
- Level
Medium5 of 10
- Topics
- BFS, Brute force, Sorting, Hash map
- Solved
- No attempts yet
Problem
You are given a permutation A1, A2, ..., AN of the N numbers from 1 to N. On this permutation you can perform the operation reverse(i, j), which reverses the order of the numbers in the range [i, j] (1 ≤ i ≤ j ≤ N). For example, applying reverse(2, 4) to [1, 2, 3, 4, 5] yields [1, 4, 3, 2, 5]. Compute the minimum number of operations needed to sort the permutation into ascending order.
Input
The input is given in the following format.
N
A1 A2 ... AN
Output
Print the answer on a single line.
Constraints
- N is an integer.
- 2 ≤ N ≤ 10