310823: CF1894D. Neutral Tonality

Memory Limit:512 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

D. Neutral Tonalitytime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

You are given an array $a$ consisting of $n$ integers, as well as an array $b$ consisting of $m$ integers.

Let $\text{LIS}(c)$ denote the length of the longest increasing subsequence of array $c$. For example, $\text{LIS}([2, \underline{1}, 1, \underline{3}])$ = $2$, $\text{LIS}([\underline{1}, \underline{7}, \underline{9}])$ = $3$, $\text{LIS}([3, \underline{1}, \underline{2}, \underline{4}])$ = $3$.

You need to insert the numbers $b_1, b_2, \ldots, b_m$ into the array $a$, at any positions, in any order. Let the resulting array be $c_1, c_2, \ldots, c_{n+m}$. You need to choose the positions for insertion in order to minimize $\text{LIS}(c)$.

Formally, you need to find an array $c_1, c_2, \ldots, c_{n+m}$ that simultaneously satisfies the following conditions:

  • The array $a_1, a_2, \ldots, a_n$ is a subsequence of the array $c_1, c_2, \ldots, c_{n+m}$.
  • The array $c_1, c_2, \ldots, c_{n+m}$ consists of the numbers $a_1, a_2, \ldots, a_n, b_1, b_2, \ldots, b_m$, possibly rearranged.
  • The value of $\text{LIS}(c)$ is the minimum possible among all suitable arrays $c$.
Input

Each test contains multiple test cases. The first line contains a single integer $t$ $(1 \leq t \leq 10^4)$ — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $n, m$ $(1 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5)$ — the length of array $a$ and the length of array $b$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \leq a_i \leq 10^9)$ — the elements of the array $a$.

The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ $(1 \leq b_i \leq 10^9)$ — the elements of the array $b$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$, and the sum of $m$ over all test cases does not exceed $2\cdot 10^5$.

Output

For each test case, output $n + m$ numbers — the elements of the final array $c_1, c_2, \ldots, c_{n+m}$, obtained after the insertion, such that the value of $\text{LIS}(c)$ is minimized. If there are several answers, you can output any of them.

ExampleInput
7
2 1
6 4
5
5 5
1 7 2 4 5
5 4 1 2 7
1 9
7
1 2 3 4 5 6 7 8 9
3 2
1 3 5
2 4
10 5
1 9 2 3 8 1 4 7 2 9
7 8 5 4 6
2 1
2 2
1
6 1
1 1 1 1 1 1
777
Output
6 5 4
1 1 7 7 2 2 4 4 5 5
9 8 7 7 6 5 4 3 2 1
1 3 5 2 4
1 9 2 3 8 8 1 4 4 7 7 2 9 6 5
2 2 1
777 1 1 1 1 1 1
Note

In the first test case, $\text{LIS}(a) = \text{LIS}([6, 4]) = 1$. We can insert the number $5$ between $6$ and $4$, then $\text{LIS}(c) = \text{LIS}([6, 5, 4]) = 1$.

In the second test case, $\text{LIS}([\underline{1}, 7, \underline{2}, \underline{4}, \underline{5}])$ = $4$. After the insertion, $c = [1, 1, 7, 7, 2, 2, 4, 4, 5, 5]$. It is easy to see that $\text{LIS}(c) = 4$. It can be shown that it is impossible to achieve $\text{LIS}(c)$ less than $4$.

Output

题目大意:
给定两个数组a和b,数组a包含n个整数,数组b包含m个整数。定义LIS(c)为数组c的最长递增子序列的长度。你需要将数组b中的数插入到数组a中,可以在任意位置以任意顺序插入,得到数组c。你的目标是使得LIS(c)最小。具体来说,就是要找到一个数组c,使得:
1. 数组a是数组c的子序列。
2. 数组c由数组a和数组b的元素组成,可能经过重新排列。
3. LIS(c)的值在所有可能的数组c中是最小的。

输入输出数据格式:
输入:
- 第一行包含一个整数t(1≤t≤10^4),表示测试用例的数量。
- 每个测试用例包含三行:
- 第一行包含两个整数n和m(1≤n≤2×10^5,1≤m≤2×10^5),分别表示数组a和数组b的长度。
- 第二行包含n个整数a_1, a_2, ..., a_n(1≤a_i≤10^9),表示数组a的元素。
- 第三行包含m个整数b_1, b_2, ..., b_m(1≤b_i≤10^9),表示数组b的元素。

输出:
- 对于每个测试用例,输出n+m个整数,表示最终数组c的元素,使得LIS(c)最小。如果有多个答案,可以输出其中任何一个。题目大意: 给定两个数组a和b,数组a包含n个整数,数组b包含m个整数。定义LIS(c)为数组c的最长递增子序列的长度。你需要将数组b中的数插入到数组a中,可以在任意位置以任意顺序插入,得到数组c。你的目标是使得LIS(c)最小。具体来说,就是要找到一个数组c,使得: 1. 数组a是数组c的子序列。 2. 数组c由数组a和数组b的元素组成,可能经过重新排列。 3. LIS(c)的值在所有可能的数组c中是最小的。 输入输出数据格式: 输入: - 第一行包含一个整数t(1≤t≤10^4),表示测试用例的数量。 - 每个测试用例包含三行: - 第一行包含两个整数n和m(1≤n≤2×10^5,1≤m≤2×10^5),分别表示数组a和数组b的长度。 - 第二行包含n个整数a_1, a_2, ..., a_n(1≤a_i≤10^9),表示数组a的元素。 - 第三行包含m个整数b_1, b_2, ..., b_m(1≤b_i≤10^9),表示数组b的元素。 输出: - 对于每个测试用例,输出n+m个整数,表示最终数组c的元素,使得LIS(c)最小。如果有多个答案,可以输出其中任何一个。

加入题单

上一题 下一题 算法标签: