No.1854 Limited Bubble Sort
Problem Statement
You are given a sequence $p$ obtained by rearranging $(1,2, \cdots , N)$. Initially, the $i$-th term of $p$ is $p_i$.
Your goal is to sort $p$ in ascending order by performing at most $N^2$ operations. You may perform no operations at all. With an operation, you can change $p$ as follows.
- Declare an integer $i$ ( $1 \leq i \leq N-1$ ) such that the $i$-th term of $p$ is not $i$ and the $i+1$-th term of $p$ is not $i+1$, and swap the $i$-th term of $p$ and the $i+1$-th term of $p$.
Determine whether you can achieve your goal, and if it is possible, show one sequence of operations that achieves it.
You are given $T$ test cases, so find the answer for each of them.
Input
The $1$st line of the input is as follows.$T$Then, $T$ test cases follow. Each of them is given in the following format.
$N$
$p_1\ \ p_2\ \ \cdots \ \ p_N$
- $1 \leq T \leq 250$
- $2 \leq N \leq 500$
- $1 \leq p_i \leq N$ ( $1 \leq i \leq N$ )
- All inputs are integers.
- $p$ is obtained by rearranging $(1,2, \cdots , N)$.
- In $1$ input file, the sum of $N$ does not exceed $500$.
Output
If you cannot achieve your goal, output -1 followed by a newline.
If you can achieve your goal, output as follows, followed by a newline.
$Q$Here, $Q$ is the number of operations, and $a_x$ is the $i$ declared in the $x$-th operation. Therefore, $0 \leq Q \leq N^2, 1 \leq a_i \leq N-1$ ( $1 \leq i \leq Q$ ) must be satisfied.
$a_1\ \ a_2\ \ \cdots \ \ a_Q$
Do this for all $T$ test cases.
Sample
Sample 1
Input
4
4
2 1 4 3
3
3 2 1
5
1 2 3 4 5
2
2 1
Output
2
3 1
-1
0
1
1
- For the $1$st test case, $p$ changes as $(2,1,4,3) \rightarrow (2,1,3,4) \rightarrow (1,2,3,4)$.
- For the $2$nd test case, no operation can be performed on $p$.
- For the $3$rd test case, $p$ is sorted in ascending order from the beginning.
- For the $4$th test case, $p$ changes as $(2,1) \rightarrow (1,2)$.
- Note that you do not need to minimize the number of operations.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
蜜蜂