Beautiful Bracelets

시간 제한1초메모리 제한256 MB

요약
n개의 조개 종류가 주어질 때, s와 t의 모든 순환 이동 사이의 최장 공통 부분 수열 중 최댓값을 최소로 하는 두 순열 s와 t를 출력한다.
난이도

어려움10점 중 8점

유형
그리디, 조합론, 정렬, 구현
정답자
아직 제출이 없습니다

문제

Busy Beaver has collected some pairs of seashells, and he is trying to make them into two beautiful bracelets!

He has NN pairs of seashells, where both seashells in the ii-th pair have type a_ia\_i. 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 ss and tt be two permutations of the original array aa. We want to find (s,t)(s,t) that minimizes the length of the longest cyclic common subsequence, f(s,t)f(s,t), defined by the following:

  • Consider all cyclic shifts of array tt, denoted as t_1,t_2,…,t_nt\_1,t\_2,\dots,t\_n2.
  • Let LCS(s,t)\text{LCS}(s,t) be the length of the Longest Common Subsequence between ss and tt. Then f(s,t)=max⁡_1≤i≤nLCS(s,t_i).f(s,t) = \max\_{1\le i \le n}\text{LCS}(s,t\_i).

Unfortunately, Busy Beaver has too many seashells to find the most beautiful bracelet pairs by hand. Please help him!


1An array ss is a subsequence of an array tt if ss can be obtained from tt by deleting some (possibly none or all) elements from tt, without reordering the remaining elements.

2A cyclic shift t_it\_i of an array t=\[t(1),t(2),…,t(n)]t=\[t^{(1)},t^{(2)},\dots,t^{(n)}] by ii places is given by \[t(1+i),t(2+i),…,t(n+i)]\[t^{(1+i)},t^{(2+i)},\dots,t^{(n+i)}], where indices are taken modulo nn.

입력

Each test contains multiple test cases. The first line of input contains a single integer TT (1≤T≤500)(1\le T\le 500), the number of test cases. The description of each test case follows.

The first line of each test case contains a single positive integer NN (1≤N≤500)(1\le N\le 500).

The second line of each test case contains NN integers a_1,a_2,…,a_Na\_1,a\_2,\dots, a\_N (1≤a_i≤109)(1\le a\_i\le 10^9) --- the types of seashells Busy Beaver has collected.

It is guaranteed that the sum of NN across all test cases does not exceed 500500.

출력

For each test case, output two lines. The first line consists of NN integers s_1,s_2,…,s_Ns\_1,s\_2,\dots,s\_N, and the second line consists of NN integers t_1,t_2,…,t_Nt\_1,t\_2,\dots,t\_N, representing some (s,t)(s,t) that minimizes f(s,t)f(s,t).

If there are multiple solutions, print any of them.

힌트

Note that f(\[1,2,3],\[1,3,2])f(\[1,2,3],\[1,3,2]) is 22 because LCS(\[1,2,3],\[1,3,2])=2\text{LCS}(\[1,2,3],\[1,3,2]) = 2 (\[1,2]\[1,2] is a shared subsequence). This is the maximum LCS\text{LCS} over all cyclic shifts of tt:

  • LCS(\[1,2,3],\[1,3,2])=2\text{LCS}(\[1,2,3],\[1,3,2]) = 2 (\[1,2]\[1,2] is a shared subsequence).
  • LCS(\[1,2,3],\[3,2,1])=1\text{LCS}(\[1,2,3],\[3,2,1]) = 1 (\[1]\[1] is a shared subsequence).
  • LCS(\[1,2,3],\[2,1,3])=2\text{LCS}(\[1,2,3],\[2,1,3]) = 2 (\[2,3]\[2,3] is a shared subsequence).

It can be shown that there are no ss and tt that are permutations of \[1,2,3]\[1,2,3], such that f(s,t)≤1f(s,t) \le 1.

예제1

  1. 예제 1

    입력
    1
    3
    1 2 3
    
    예상 출력
    1 2 3
    1 3 2