Moves You Need to Make
Time limit2sMemory limit512 MB
Given a permutation, find the fewest moves to sort it using adjacent swaps plus at most one swap of the first and last elements.
- Level
Medium6 of 10
- Topics
- Array, Math, Implementation
- Solved
- No attempts yet
Problem
You are given a permutation.
A move is one of the following:
- Swap two adjacent elements.
- Swap the first and the last elements. Can be used at most once.
What is the minimum number of moves you need to make to sort the given permutation?
Input
The first line contains a single integer (), the length of the permutation.
The second line contains integers (), the permutation itself.
Output
Output a single integer: the minimum number of moves you need to make to sort the given permutation.