欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1918B.Minimize Inversions
Minimize Inversions
You are given two permutations and of length . A permutation is an array of elements from to where all elements are distinct. For example, an array [] is a permutation, but [] and [] aren't.
You can (as many times as you want) choose two indices and , then swap with and with simultaneously.
You hate inversions, so you want to minimize the total number of inversions in both permutations.
An inversion in a permutation is a pair of indices such that and . For example, if then there are inversions in it (the pairs of indices are , and ).
Input
The first line contains an integer () — the number of test cases.
Each test case consists of three lines. The first line contains an integer () — the length of the permutations and . The second line contains integers () — permutation . The third line contains in a similar format.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output two permutations and (in the same format as in the input) — the permutations after all operations. The total number of inversions in and should be the minimum possible among all pairs of permutations that can be obtained using operations from the statement.
If there are multiple solutions, print any of them.
Note
In the first test case, the minimum possible number of inversions is .
In the second test case, we can sort both permutations at the same time. For this, the following operations can be done:
- Swap the elements in the positions and in both permutations. After the operation, [], [].
- Swap the elements in the positions and . After the operations, and are sorted.
In the third test case, the minimum possible number of inversions is .
Samples
3
5
1 2 3 4 5
5 4 3 2 1
3
3 1 2
3 1 2
6
2 5 6 1 3 4
1 5 3 6 2 4
3 2 5 1 4
3 4 1 5 2
1 2 3
1 2 3
2 3 4 6 5 1
1 2 4 3 5 6
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |