#DSU0000059. Đơn giản hóa hoán vị (Sakurako, Kosuke, and the Permutation)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đơn giản hóa hoán vị (Sakurako, Kosuke, and the Permutation)
Sakurako, Kosuke, and the Permutation
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of . A permutation is called simple if, for every index , at least one of and holds. Thus, an element either stays in its original position or belongs to a pair of positions whose values point to each other.
In one operation, choose any two positions and swap their current values. The positions need not be adjacent, and each swap counts as one operation. Every intermediate array remains a permutation.
Find the minimum number of swaps needed to obtain a simple permutation.
Input
The first line contains ; the next line contains .
Output
Print the minimum number of swaps.
Subtasks
- Subtask 1 (20 points): ; is a permutation of .
- Subtask 2 (30 points): ; is a permutation of .
- Subtask 3 (50 points): ; is a permutation of .
Examples
Input
5
2 3 4 5 1
Output
2
Explanation
The permutation is one cycle of length 5. Making all cycles have length 1 or 2 requires swaps.