K-Sort

Points 100 3.0s 256M

Kurumi found a cipher made from a permutation and needs your help.

There is a permutation \(P\) of length \(N\). One \(K\)-insert operation chooses an integer \(x\), removes \(x\) from the current permutation, and inserts \(x\) after the first \(K\) elements of the remaining permutation. In particular, if \(K = 0\), then \(x\) is inserted at the beginning.

A solution is a sequence of \(K\)-insert operations that turns \(P\) into the identity permutation \([1,2,\ldots,N]\). Output a shortest possible operation sequence. If there are multiple shortest sequences, output any one of them.

Input

The first line contains an integer \(T\), the number of test cases.

Each test case contains two lines. The first line contains two integers \(N\) and \(K\). The second line contains \(N\) integers, representing the permutation \(P\).

Constraints

  • \(1 \le T \le 10^6\)
  • \(1 \le N \le 10^6\)
  • \(0 \le K \le N - 1\)
  • \(P\) is a permutation of \(1,2,\ldots,N\).
  • The sum of \(N\) over all test cases does not exceed \(10^6\).

Output

For each test case, output two lines.

On the first line, output an integer \(M\), the length of your operation sequence.

On the second line, output \(M\) space-separated integers, the chosen value \(x\) for each operation in order. If \(M = 0\), this line may be empty.

Scoring

This problem uses a special checker. For each test case, if the output is ill-formatted or the operations do not transform the permutation into \([1,2,\ldots,N]\), that test case receives no points.

Let \(M'\) be the shortest possible solution length.

  • If \(M = M'\), the test case receives full points.
  • If \(M' < M \le 2M'\), the test case receives half points.
  • Otherwise, the test case receives no points.

The subtasks are:

  • Subtask 1 (10 points): \(K = 0\).
  • Subtask 2 (90 points): no additional constraints.

Sample Input 1

1
4 1
2 3 4 1

Sample Output 1

2
1 2

Sample Explanation 1

After choosing \(1\) for the first operation, the permutation becomes \([2,1,3,4]\).

After choosing \(2\) for the second operation, the permutation becomes \([1,2,3,4]\). It can be proven that no shorter solution exists.

Sample Input 2

1
5 0
2 1 3 4 5

Sample Output 2

1
1

Sample Explanation 2

Since \(K = 0\), every operation moves the chosen value to the beginning. Moving only \(1\) produces the identity permutation.

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.