D. Neutral Tonality

发布时间 2023-11-10 10:07:41作者: onlyblues

D. Neutral Tonality

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.

Example

Input

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$.

 

解题思路

  比赛的时候猜对了做法,现在补个证明。

  由于序列 $a$ 中的元素在 $c$ 中的相对顺序不变,因此必然必然有 $\text{LIS}(c) \geq \text{LIS}(a)$。另外对序列 $b$ 降序排序依次插到 $a$ 中,并保持序列 $b$ 的元素在 $c$ 中从左到右递降的相对顺序。那么就会有 $\text{LIS}(c) \leq \text{LIS}(a) + 1$,这是因为 $c$ 的任意上升子序列中最多只会含有 $b$ 中的一个元素。

  下面给出使得 $\text{LIS}(c) = \text{LIS}(a)$ 的构造方案,由于 $c$ 的最长上升子序列取决于 $a$,因此关键是在插入 $b$ 中的元素时不让 $\text{LIS}(a)$ 变大即可。

  当插入的元素 $x \leq \min\limits_{1 \leq i \leq n}{a_i}$,那么直接将 $x$ 插入到 $a$ 的末尾即可。$x$ 必然不会出现在长度至少为 $2$ 的上升子序列中,因此必然不会导致 $\text{LIS}(a)$ 变大。

  否则找到满足 $a_i \leq x$ 的最小的下标 $i$,并将 $x$ 插在 $i$ 之前。那么在最长上升子序列中 $x$ 和 $a_i$ 最多只会含有一个,而如果该最长上升子序列含有 $x$,那么将 $x$ 替换成 $a_i$ 仍然是最长上升子序列,且该最长上升子序列不再含有 $b$ 中的元素(参考上面提到的 $c$ 中最多含有 $b$ 的一个元素),因此 $\text{LIS}(a)$ 不会增加。之所以要在最小的下标前插入,是为了保证 $b$ 在 $c$ 中降序的相对顺序。当 $b$ 中较大的元素 $b_i$ 插入到 $a$ 中后,那么之后较小的元素 $b_j$ 不可能再插到 $b_i$ 的前面,若插入 $a$ 后 $b_i$ 后面不存在比 $b_j$ 小的元素,而前面存在比 $b_j$ 小的元素(即 $b_i$ 不是插在最小的下标前面),那么 $c$ 的最长上升子序列必然会选择 $b_j$,有 $\text{LIS}(c) = \text{LIS}(a) + 1$。

  实现的话只需开两个指针维护 $a$ 和 $b$,如果 $a_i < b_j$,那么将 $a_i$ 加到 $c$ 中,然后 $i$ 移到下一个位置。如果 $b_j \geq a_i$,那么将 $b_j$ 加到 $c$ 中,然后 $j$ 移到下一个位置。直到有一个序列的元素都加入到 $c$ 中,最后只需把另外一个序列的剩余元素依次加到 $c$ 中即可。

  AC 代码如下,时间复杂度为 $O(n + m \log m)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 10;

int a[N], b[N];

void solve() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
    	scanf("%d", a + i);
    }
    for (int i = 0; i < m; i++) {
    	scanf("%d", b + i);
    }
    sort(b, b + m, greater<int>());
    vector<int> ans;
    for (int i = 0, j = 0; i < n || j < m; ) {
    	if (i < n && j < m) {
    		if (a[i] >= b[j]) ans.push_back(a[i++]);
    		else ans.push_back(b[j++]);
    	}
    	else if (i < n) {
    		ans.push_back(a[i++]);
    	}
    	else if (j < m) {
    		ans.push_back(b[j++]);
    	}
    }
    for (auto &x : ans) {
    	printf("%d ", x);
    }
    printf("\n");
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		solve();
	}
	
	return 0;
}

 

参考资料

  Codeforces Round 908 (Div. 1, Div, 2) Editorial:https://codeforces.com/blog/entry/122074