个人技术分享

C - Sort (atcoder.jp)

思路

就按举的例子来说:

输入:

5
34125

输出:

2
1 3
2 4

The operations change the sequence as follows:

  • Initially, A=(3,4,1,2,5).
  • The first operation swaps the first and third elements, making =(1,4,3,2,5).
  • The second operation swaps the second and fourth elements, making A=(1,2,3,4,5).

Other outputs such as the following are also considered correct:

4
2 3
3 4
1 2
2 3
A数组 3 4 1 2 5

目标格局

1 2 3 4 5
映射数组(对应数组) 1->3 2->4 3->1 4->2 5->5

我们通过A数组以及目标格局采用线性扫描的方式(复杂度O(N)),把原来的数字与现在的位置下标对应,写出映射数组。

我们尝试把映射数组里按照升序还原,并在此期间改变A数组。

3 4 1 2 5
(1)1 4 3 2 5
(2)1 2 3 4 5
-END-

我们可以发现这样做是可行的!

再是一个样例:

输入:

3
3 1 2

尝试:

3 1 2
(1)1 3 2
(2)1 2 3
-END-

答案是2,与标准一致。

我们只需在把变换过程输出就行了~

(见后续---)