#P7406. [JOI 2021 Final] 集体照 / Group Photo
[JOI 2021 Final] 集体照 / Group Photo
Description
There are people, numbered from to . The height of person is .
There are steps, numbered from to from lowest to highest. Step is lower than step by units of height. Each step can hold at most one person. Person stands on step .
You may perform the following operation any number of times:
- Choose and swap the person on step with the person on step .
Suppose the height of the person standing on step is . You must make the arrangement satisfy:
- For every , .
Find the minimum number of operations.
Input Format
The first line contains an integer , the number of people.
The second line contains integers , meaning that person stands on step .
Output Format
Print one integer, the minimum number of operations.
5
3 5 2 4 1
3
5
3 2 1 5 4
0
9
6 1 3 4 9 5 7 8 2
9
Hint
Explanation for Sample 1
Let be the height of the person standing on step :
- Swap person and person , .
- Swap person and person , .
- Swap person and person , .
After exactly steps, the condition is satisfied.
Explanation for Sample 2
The condition is already satisfied, so no operations are needed.
Constraints
This problem uses bundled testdata.
- Subtask 1 (5 pts): .
- Subtask 2 (7 pts): .
- Subtask 3 (32 pts): .
- Subtask 4 (20 pts): .
- Subtask 5 (36 pts): no additional constraints.
For all testdata, , , and all are distinct.
Note
Translated from The 20th Japanese Olympiad in Informatics Final Round C 集合写真 English version: Group Photo。
Translated by ChatGPT 5
京公网安备 11011102002149号