問題一覧 > 通常問題

No.1854 Limited Bubble Sort

レベル : / AI推定難易度 : 2050 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (AC するまで非表示) / 解いたユーザー数 21
作問者 : 蜜蜂 / テスター : Mitarushi
お気に入りにしたユーザー ProblemId : 6727 / 自分の提出
問題文最終更新日: 2022-02-26 00:09:30
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 333 (灘高入試コンテスト2022) (順位表) の他の問題:

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$
$a_1\ \ a_2\ \ \cdots \ \ a_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.

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もしくは右上の雲マークをクリックしてアカウントを作成してください。