#P1774. 最接近神的人

    ID: 732 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>线段树树状数组递归NOI 导刊排序

最接近神的人

Output Format

Output a single integer ans\mathit{ans}, the minimum number of operations.

4
2 8 0 3

3

Hint

Constraints

  • For 30%30\% of the testdata, 1n1041 \le n \le 10^4.
  • For 100%100\% of the testdata, 1n5×1051 \le n \le 5 \times 10^5, Ai[231,231)A_i \in [-2^{31}, 2^{31}).

Sample Explanation

The initial sequence is [2,8,0,3][2,8,0,3], and the target sequence is [0,2,3,8][0, 2, 3, 8]. One way to reach the target in three operations:

  1. Swap (8,0)(8,0), the sequence becomes [2,0,8,3][2,0,8,3].
  2. Swap (2,0)(2,0), the sequence becomes [0,2,8,3][0,2,8,3].
  3. Swap (8,3)(8,3), the sequence becomes [0,2,3,8][0,2,3,8].

Translated by ChatGPT 5