K-Sort
Points 100 3.0s 256MKurumi 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.
Log in to write and submit code.
Log in