QOJ.ac

QOJ

حد الوقت: 2.0 s حد الذاكرة: 512 MB مجموع النقاط: 100

#18048. 你需要进行的移动

الإحصائيات

给你一个排列。

一次操作可以是以下之一:

  1. 交换两个相邻的元素。
  2. 交换第一个和最后一个元素。该操作最多只能使用一次。

要将给定的排列排序,最少需要进行多少次操作?

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 3 \cdot 10^5$),表示排列的长度。

第二行包含 $n$ 个整数 $a_i$ ($1 \le a_i \le n$),表示排列本身。

输出格式

输出一个整数,表示将给定排列排序所需的最少操作次数。

样例

输入样例 1

1
1

输出样例 1

0

输入样例 2

2
1 2

输出样例 2

0

输入样例 3

3
3 2 1

输出样例 3

1

输入样例 4

4
4 2 1 3

输出样例 4

2

输入样例 5

5
4 1 5 3 2

输出样例 5

4

输入样例 6

6
1 5 3 4 2 6

输出样例 6

5

输入样例 7

7
3 2 1 7 6 5 4

输出样例 7

9

输入样例 8

8
4 2 6 1 5 3 7 8

输出样例 8

8

输入样例 9

9
9 8 7 6 5 4 3 2 1

输出样例 9

22

输入样例 10

10
8 2 9 5 1 7 10 4 6 3

输出样例 10

17

输入样例 11

11
7 2 3 9 11 1 8 6 4 10 5

输出样例 11

23

输入样例 12

12
3 10 6 2 4 12 7 8 5 1 11 9

输出样例 12

18

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.