#P9347. 似曾相识燕归来
似曾相识燕归来
Background
As the spring rain is about to end, a swallow suddenly chirps softly, stirring up ripples on the spring water. The ripples bring back memories from last year. The memories have a fragrance, filling the eaves. Under the eaves, the old swallow nest is worn and broken. Tears cannot help but fall. Looking up at the returning swallows, something moves in the heart, as if they are familiar faces from long ago.
Problem Description
swallows fly across the sunset. In order from front to back, the size of the -th swallow is , and is a permutation of length .
Now you may perform at most operations of the following type:
- Choose three integers such that . If , swap the -th and -th swallows; otherwise, swap the -th and -th swallows.
To make the formation neat, we want the swallows to be in increasing order from front to back, that is, for all .
Determine whether the goal can be achieved. If yes, construct a sequence of operations that satisfies the requirements.
Input Format
This problem contains multiple test cases.
The first line contains an integer , the number of test cases.
For each test case:
- The first line contains two integers .
- The second line contains integers .
Output Format
For each test case:
- If it is impossible to make increasing using at most operations, output one line
-1. - Otherwise, output an integer on the first line, indicating the number of operations. Then output lines, each containing three integers , describing an operation. You must guarantee that and .
1
4 4
4 2 1 3
2
1 3 4
2 3 4
Hint
[Hint]
A permutation of length is an array in which every positive integer from to appears exactly once. For example, is a permutation of length , while is not a permutation.
[Explanation for Sample 1]
- In the first operation, . Since , we swap and , and now .
- In the second operation, . Since , we swap and , and now .
[Constraints]
This problem uses bundled testdata.
- Subtask 1 (5 points): .
- Subtask 2 (5 points): .
- Subtask 3 (5 points): , .
- Subtask 4 (10 points): .
- Subtask 5 (25 points): .
- Subtask 6 (25 points): .
- Subtask 7 (25 points): no special restrictions.
For of the testdata, , , , and is a permutation of .
Translated by ChatGPT 5