No.2289 Permutation Sort
問題文最終更新日: 2023-05-05 21:12:19
English (AI translation)
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence.
Back to the original
Problem Statement
You are given a permutation $ P = (P_{1}, P_{2}, P_{3}, \cdots, P_{N})$ consisting of $N$ distinct elements from $1$ to $N$.
You can perform the following operation on the permutation $P$.
- Choose integers $i$ , $j$ ($1 \leq i < j \leq N$) and swap $P_{i}$ and $P_{j}$.
Input
$N$
$P_{1}$ $P_{2}$ $P_{3}$ $\cdots$ $P_{N}$
- $1 \leq N \leq 100$
- $1 \leq P_{i} \leq N$
- For $1 \leq i, j \leq N$, if $i \neq j$ holds, then $P_{i} \neq P_{j}$ holds.
- All inputs are integers.
Output
Output in one line the minimum number of operations required to sort the given permutation $P$ in ascending order. Print a newline at the end.
Sample
Sample 1
Input
3 3 1 2
Output
2
It can be sorted in ascending order with 2 operations, for example by rearranging as $(3, 1, 2)\ \to\ (1, 3, 2)\ \to (1, 2, 3)$.
Sample 2
Input
6 3 1 6 5 4 2
Output
4
Sample 3
Input
1 1
Output
0
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。