AtCoder Regular Contest 225
D
间隔交换(简单版)
Gap Swap (easy)
AtCoder
⌀ 1534
原题
← CE →

间隔交换(简单版)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}$。
AI (CI翻译)

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)$ 的排列。
  • 所有输入值均为整数。
AI (CI翻译)

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_N
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output the answer.

输出答案。AI (CI翻译)

样例

Input #1
6
6 2 3 5 4 1
Output #1
6
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 $P_4, P_5$. This costs $1$. - Second operation: choose $i=1, j=6$, and swap $P_1, P_6$. This costs $5$. $P$ cannot be sorted into ascending order with a cost less than $6$, so the answer is $6$.
Input #2
3
1 2 3
Output #2
0
The required total cost can be $0$.
题目信息
题号
arc225_d
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。