CF2229D.Me When Median Problem
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two arrays of positive integers a and b, both of length n. You will perform the following operation exactly n−1 times:
- let m be the current length of a and b, note that the lengths will always be equal.
- select an integer i (1≤i<m):
- let S be the multiset ai,ai+1,bi,bi+1
- sort the elements of S such that s1≤s2≤s3≤s4.
- now replace ai,ai+1 with s2 and bi,bi+1 with s3. More formally, replace a with [a1,a2,…,ai−1,s2,ai+2,…,am], and replace b with [b1,b2,…,bi−1,s3,bi+2,…,bm].
After performing all operations, there will be exactly 1 element remaining in both a and b. Determine the maximum value of min(a1,b1) attainable if you perform operations optimally.
给你两个长度均为 n 的正整数数组 a 和 b。你将恰好执行以下操作 n−1 次:
- 设 m 为当前 a 和 b 的长度(注意:两数组长度始终相等);
- 选择一个整数 i(满足 1≤i<m):
- 令 S 为多重集 {ai,ai+1,bi,bi+1};
- 将 S 中的元素升序排序,得到 s1≤s2≤s3≤s4;
- 现在将 ai,ai+1 替换为 s2,并将 bi,bi+1 替换为 s3。更准确地说,将 a 替换为 [a1,a2,…,ai−1,s2,ai+2,…,am],将 b 替换为 [b1,b2,…,bi−1,s3,bi+2,…,bm]。
执行完所有操作后,a 和 b 中均恰好剩余 1 个元素。若你可以最优地执行所有操作,求最终 min(a1,b1) 的最大可能值。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each testcase contains an integer n (1≤n≤105) — the length of the arrays a and b.
The second line of each testcase contains n integers a1,a2,…,an (1≤ai≤2⋅n).
The third line of each testcase contains n integers b1,b2,…,bn (1≤bi≤2⋅n).
It is guaranteed that the sum of n over all test cases does not exceed 105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤105)—— 表示数组 a 和 b 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤2⋅n)。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(1≤bi≤2⋅n)。
保证所有测试用例中 n 的总和不超过 105。
输出格式
For each testcase, output the maximum value of min(a1,b1) attainable.
对于每个测试用例,输出可达到的 min(a1,b1) 的最大值。
输入输出样例
输入#1
6 1 1 2 3 2 4 5 1 3 6 4 7 5 4 8 4 6 7 8 8 8 7 13 11 1 10 4 5 11 11 12 8 9 2 3 13 9 16 1 9 12 5 18 10 10 16 14 6 7 11 12 17 18 3 17 6 3 6 12 4 10 12 2 3 2 7 8 9
输出#1
1 3 6 8 14 8
说明/提示
In the first example, we do not need to perform any operations, so the answer is just min(1,2) which is 1.
For the second example, we can do the following sequence of moves:
- select i=1 and then:
- S=2,4,1,3, s1=1, s2=2, s3=3, s4=4
- a=[2,4,5]→[2,5]
- b=[1,3,6]→[3,6]
- select i=1 and then
- S=2,5,3,6, s1=2, s2=3, s3=5, s4=6
- a=[2,5]→[3]
- b=[3,6]→[5]
The answer is then min(3,5) which is 3, it can be proven that this is optimal.
在第一个例子中,我们无需执行任何操作,因此答案即为 min(1,2),也就是 1。
对于第二个例子,我们可以执行以下操作序列:
- 选择 i=1,然后:
- S=2,4,1,3,s1=1,s2=2,s3=3,s4=4
- a=[2,4,5]→[2,5]
- b=[1,3,6]→[3,6]
- 再次选择 i=1,然后:
- S=2,5,3,6,s1=2,s2=3,s3=5,s4=6
- a=[2,5]→[3]
- b=[3,6]→[5]
此时答案为 min(3,5),即 3;可以证明该结果是最优的。
输入解题思路,AI测评打分。不知道怎么写?