#P8197. [传智杯 #4 决赛] 排排队
[传智杯 #4 决赛] 排排队
Problem Description
cyq works as a PE teacher at tsyz and is responsible for lining students up.
In tsyz, each person has a height , and only two adjacent people are allowed to swap positions. The team led by cyq has people, and he now wants to arrange them into a formation.
Given a sequence of length , a formation is considered beautiful if and only if for all , . cyq wants to know whether he can make the formation beautiful, and the number of adjacent swaps does not exceed . This problem has stumped . Please help solve it: if there exists a valid swapping plan, output YES and provide one plan; otherwise, output NO.
Input Format
This problem contains multiple test cases within a single test point.
The first line contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer, representing the team length .
The second line contains integers; the -th integer is the height of the -th person.
The third line contains integers; the -th integer is the height of the -th person in the beautiful formation.
Output Format
For each test case, output the answer in order.
For each test case, if there exists a plan, output YES on the first line; otherwise, output NO.
If you output YES, then output several lines, each containing two integers , meaning that the -th student and the -th student swap positions. Obviously, . After finishing all swaps, you must output one line 0 0 to indicate the end of your operations. Note that the array indices are numbered from 1 to .
If you output NO, then you do not need to output anything afterward.
Please pay special attention: for each test case, the number of operations must not exceed (not including the line 0 0), otherwise you will get WA (Wrong Answer).
3
4
1 2 2 3
3 2 2 1
3
1 2 3
1 2 4
1
1
1
YES
4 3
2 3
1 2
3 2
3 4
0 0
NO
YES
0 0
Hint
Constraints
For all test points, it is guaranteed that , , , and the sum of over all test cases does not exceed , i.e. .
Notes
- Please note that a large amount of output may affect program efficiency, so do not flush the buffer too often. For example, C++ users using
std::coutshould use'\n'instead ofstd::endlfor newlines; Java users should choose efficient output methods such as usingPrintWriter; Python users can useprintnormally without worrying about efficiency. - Please output your answer according to the required output format. If the format is incorrect, the judging feedback may be TLE, RE, WA, UKE, or any other result.
Example of efficient output in C++
#include <iostream>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
for (int i = 1; i <= 5; ++i) {
std::cout << i << '\n'; // Note that std::endl cannot be used here
}
}
Example of efficient output in Java
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
PrintWriter ot = new PrintWriter(System.out);
for (int i = 1; i <= 5; ++i) {
ot.println(i);
}
ot.flush(); // Make sure to run this line when the program ends, otherwise the buffer content cannot be output
}
}
Translated by ChatGPT 5