Type: Default File IO: sort 1000ms 512MiB

排序

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

题目描述

黑哥有一个长度为 nn 的排列 a1,a2,…,ana_1,a_2,\dots,a_n。他可以花费 11 来进行下面的操作:

  • 选择 i,ji,j,满足:1≤i<j≤n1\le i<j\le n 且对于所有 i<k<ji<k<j 都有 ak=ka_k=k;
  • 交换 (ai,aj)(a_i,a_j)。

求黑哥把排列变成升序的最小花费。

输入格式

从文件 sort.in 中读入数据。

输入的第一行包含一个正整数 nn,表示排列长度。

输入的第二行包含 nn 个正整数,其中第 ii 个正整数为 aia_i。

输出格式

输出到文件 sort.out 中。

输出一行一个非负整数,表示把排列变成升序的最小花费。

6
6 2 3 5 4 1
2

样例 1 解释

两次操作分别选择 i=4,j=5i=4,j=5 和 i=1,j=6i=1,j=6。可以证明无法使用少于两次操作完成排序。

样例 2

见附件中的 sort2.in 与 sort2.ans。

数据范围

对于所有测试数据,保证:1≤n≤5×1051\leq n\leq 5\times 10^5,1≤ai≤n1\leq a_i\leq n,a1∼ana_1\sim a_n 为 1∼n1\sim n 的排列。

测试点 n≤n\le
1∼41\sim 4 1010
5∼75\sim 7 2020
8∼108\sim 10 10001000
11∼2011\sim 20 5×1055\times 10^5

附件

公开样例打包下载 · 原题面 PDF

20261020-赛后改题

Not Claimed
Status
Done
Problem
4
Open Since
2026-10-20 13:00
Deadline
2026-10-27 23:59
Extension
0 hour(s)