問題一覧 > 通常問題

No.2289 Permutation Sort

レベル : / AI推定難易度 : 700 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 179
作問者 : t98slider / テスター : 👑 p-adic
お気に入りにしたユーザー ProblemId : 9123 / 自分の提出
問題文最終更新日: 2023-05-05 21:12:19
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
yukicoder contest 387 (Union Find Contest) (順位表) の他の問題:

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}$.
Output the minimum number of operations required to sort the given permutation $P$ in ascending order.

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もしくは右上の雲マークをクリックしてアカウントを作成してください。