间隔交换(简单版)AI (CI翻译)
Problem Statement
题目描述
AI (CI翻译)This problem has a similar setting to Problem E, but the cost of the operation is different.
本题与问题 E 的设定类似,但操作的代价不同。AI (CI翻译)
There is a permutation $P$ of $(1,2,\ldots,N)$. You can perform the following operation on $P$.
给定一个 $(1,2,\ldots,N)$ 的排列 $P$。你可以对 $P$ 执行以下操作。AI (CI翻译)
- Choose integers
$i$and$j$satisfying$1 \le i < j \le N$. Here,$P_k=k$must hold for every integer$k$satisfying$i < k < j$. Then, swap$P_i$and$P_j$. This operation incurs a cost of$\boldsymbol{j-i}$.
- 选择满足
$1 \le i < j \le N$的整数$i$和$j$。对于满足$i < k < j$的每个整数$k$,必须有$P_k=k$。然后交换$P_i$和$P_j$。执行该操作的代价为$\boldsymbol{j-i}$。
Note that if $i+1=j$, there is no integer $k$ satisfying $i<k<j$, so the operation can always be performed. Therefore, there always exists a sequence of operations that sorts $P$ into ascending order.
注意,当 $i+1=j$ 时,不存在满足 $i<k<j$ 的整数 $k$,因此该操作总是可以执行。所以,总是存在一系列操作能够将 $P$ 排序为升序。AI (CI翻译)
Find the minimum total cost required to sort $P$ into ascending order.
求将 $P$ 排序为升序所需的最小总代价。AI (CI翻译)
Constraints
约束条件
AI (CI翻译)$2 \le N \le 5\times 10^5$$1 \le P_i \le N$$P$is a permutation of$(1,2,\ldots,N)$.- All input values are integers.
$2 \le N \le 5\times 10^5$$1 \le P_i \le N$$P$是$(1,2,\ldots,N)$的排列。- 所有输入值均为整数。
Input
输入格式
AI (CI翻译)The input is given from Standard Input in the following format:
输入从标准输入中以以下格式给出:AI (CI翻译)
N
P_1 P_2 \ldots P_N
N
P_1 P_2 \ldots P_NAI (CI翻译)
Output
输出格式
AI (CI翻译)Output the answer.
输出答案。AI (CI翻译)
样例
6
6 2 3 5 4 1
6
3
1 2 3
0
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。