Beautiful Bracelets
시간 제한1초메모리 제한256 MB
n개의 조개 종류가 주어질 때, s와 t의 모든 순환 이동 사이의 최장 공통 부분 수열 중 최댓값을 최소로 하는 두 순열 s와 t를 출력한다.
문제
Busy Beaver has collected some pairs of seashells, and he is trying to make them into two beautiful bracelets!
He has pairs of seashells, where both seashells in the -th pair have type . He wants to make two bracelets, such that each bracelet has one seashell from each pair. Busy Beaver decides his own metric for a beautiful pair of bracelets, which is minimizing the length of the longest common subsequence1 between two bracelets.
Formally, let and be two permutations of the original array . We want to find that minimizes the length of the longest cyclic common subsequence, , defined by the following:
- Consider all cyclic shifts of array , denoted as 2.
- Let be the length of the Longest Common Subsequence between and . Then
Unfortunately, Busy Beaver has too many seashells to find the most beautiful bracelet pairs by hand. Please help him!
1An array is a subsequence of an array if can be obtained from by deleting some (possibly none or all) elements from , without reordering the remaining elements.
2A cyclic shift of an array by places is given by , where indices are taken modulo .
입력
Each test contains multiple test cases. The first line of input contains a single integer , the number of test cases. The description of each test case follows.
The first line of each test case contains a single positive integer .
The second line of each test case contains integers --- the types of seashells Busy Beaver has collected.
It is guaranteed that the sum of across all test cases does not exceed .
출력
For each test case, output two lines. The first line consists of integers , and the second line consists of integers , representing some that minimizes .
If there are multiple solutions, print any of them.
힌트
Note that is because ( is a shared subsequence). This is the maximum over all cyclic shifts of :
- ( is a shared subsequence).
- ( is a shared subsequence).
- ( is a shared subsequence).
It can be shown that there are no and that are permutations of , such that .