D. Neutral Tonality

D. Neutral Tonality

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

Let LIS(c) denote the length of the longest increasing subsequence of array c. For example, LIS([2,1_,1,3_]) = 2, LIS([1_,7_,9_]) = 3, LIS([3,1_,2_,4_]) = 3.

You need to insert the numbers b1,b2,,bm into the array a, at any positions, in any order. Let the resulting array be c1,c2,,cn+m. You need to choose the positions for insertion in order to minimize LIS(c).

Formally, you need to find an array c1,c2,,cn+m that simultaneously satisfies the following conditions:

  • The array a1,a2,,an is a subsequence of the array c1,c2,,cn+m.
  • The array c1,c2,,cn+m consists of the numbers a1,a2,,an,b1,b2,,bm, possibly rearranged.
  • The value of 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 (1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n,m (1n2105,1m2105) — the length of array a and the length of array b.

The second line of each test case contains n integers a1,a2,,an (1ai109) — the elements of the array a.

The third line of each test case contains m integers b1,b2,,bm (1bi109) — the elements of the array b.

It is guaranteed that the sum of n over all test cases does not exceed 2105, and the sum of m over all test cases does not exceed 2105.

Output

For each test case, output n+m numbers — the elements of the final array c1,c2,,cn+m, obtained after the insertion, such that the value of 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, LIS(a)=LIS([6,4])=1. We can insert the number 5 between 6 and 4, then LIS(c)=LIS([6,5,4])=1.

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

 

解题思路

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

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

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

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

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

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

  AC 代码如下,时间复杂度为 O(n+mlogm)

#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

posted @   onlyblues  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示