AT_arc225_d.Gap Swap (easy)
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This problem has a similar setting to Problem E, but the cost of the operation is different.
There is a permutation P of (1,2,…,N). You can perform the following operation on P.
- Choose integers i and j satisfying 1≤i<j≤N. Here, Pk=k must hold for every integer k satisfying i<k<j. Then, swap Pi and Pj.
This operation incurs a cost of 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.
Find the minimum total cost required to sort P into ascending order.
本题的设定与问题 E 类似,但操作的代价不同。
给定一个 (1,2,…,N) 的排列 P。你可以对 P 执行以下操作:
- 选择满足 1≤i<j≤N 的整数 i 和 j,要求对每个满足 i<k<j 的整数 k,均有 Pk=k。然后交换 Pi 和 Pj。
此操作的代价为 j−i。
注意:若 i+1=j,则不存在满足 i<k<j 的整数 k,因此该操作总可执行。故总存在一系列操作能将 P 排序为升序。
求将 P 排序为升序所需的最小总代价。
输入格式
The input is given from Standard Input in the following format:
N
P1 P2 … PN
输入从标准输入给出,格式如下:
N
P1 P2 … PN
输出格式
Output the answer.
输出答案。
输入输出样例
输入#1
6 6 2 3 5 4 1
输出#1
6
输入#2
3 1 2 3
输出#2
0
说明/提示
Sample 1 Explanation:
For example, by performing two operations as follows, P can be sorted into ascending order with a total cost of 6.
- First operation: choose i=4,j=5, and swap P4,P5. This costs 1.
- Second operation: choose i=1,j=6, and swap P1,P6. This costs 5.
P cannot be sorted into ascending order with a cost less than 6, so the answer is 6.
Sample 2 Explanation:
The required total cost can be 0.
Constraints
- 2≤N≤5×105
- 1≤Pi≤N
- P is a permutation of (1,2,…,N).
- All input values are integers.
样例 1 解释:
例如,通过执行以下两次操作,可将 P 排序为升序,总代价为 6。
- 第一次操作:选择 i=4,j=5,交换 P4 与 P5。此次操作代价为 1。
- 第二次操作:选择 i=1,j=6,交换 P1 与 P6。此次操作代价为 5。
无法以小于 6 的总代价将 P 排序为升序,因此答案为 6。
样例 2 解释:
所需总代价可以为 0。
约束条件
- 2≤N≤5×105
- 1≤Pi≤N
- P 是 (1,2,…,N) 的一个排列。
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?