CF2229D.Me When Median Problem

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two arrays of positive integers aa and bb, both of length nn. You will perform the following operation exactly n−1n - 1 times:

  • let mm be the current length of aa and bb, note that the lengths will always be equal.
  • select an integer ii (1≤i<m1 \le i \lt m):
    • let SS be the multiset ai,ai+1,bi,bi+1{a_i, a_{i + 1}, b_i, b_{i + 1}}
    • sort the elements of SS such that s1≤s2≤s3≤s4s_1 \le s_2 \le s_3 \le s_4.
    • now replace ai,ai+1a_i, a_{i + 1} with s2s_2 and bi,bi+1b_i, b_{i + 1} with s3s_3. More formally, replace aa with [a1,a2,…,ai−1,s2,ai+2,…,am][a_1,a_2,\ldots,a_{i - 1},s_2,a_{i + 2},\ldots,a_m], and replace bb with [b1,b2,…,bi−1,s3,bi+2,…,bm][b_1,b_2,\ldots,b_{i - 1},s_3,b_{i + 2},\ldots,b_m].

After performing all operations, there will be exactly 11 element remaining in both aa and bb. Determine the maximum value of min⁡(a1,b1)\min(a_1, b_1) attainable if you perform operations optimally.

给你两个长度均为 nn 的正整数数组 aa 和 bb。你将恰好执行以下操作 n−1n - 1 次:

  • 设 mm 为当前 aa 和 bb 的长度(注意:两数组长度始终相等);
  • 选择一个整数 ii(满足 1≤i<m1 \le i < m):
    • 令 SS 为多重集 {ai,ai+1,bi,bi+1}\{a_i, a_{i + 1}, b_i, b_{i + 1}\};
    • 将 SS 中的元素升序排序,得到 s1≤s2≤s3≤s4s_1 \le s_2 \le s_3 \le s_4;
    • 现在将 ai,ai+1a_i, a_{i + 1} 替换为 s2s_2,并将 bi,bi+1b_i, b_{i + 1} 替换为 s3s_3。更准确地说,将 aa 替换为 [a1,a2,…,ai−1,s2,ai+2,…,am][a_1,a_2,\ldots,a_{i - 1},s_2,a_{i + 2},\ldots,a_m],将 bb 替换为 [b1,b2,…,bi−1,s3,bi+2,…,bm][b_1,b_2,\ldots,b_{i - 1},s_3,b_{i + 2},\ldots,b_m]。

执行完所有操作后,aa 和 bb 中均恰好剩余 11 个元素。若你可以最优地执行所有操作,求最终 min⁡(a1,b1)\min(a_1, b_1) 的最大可能值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each testcase contains an integer nn (1≤n≤1051 \le n \le 10^5) — the length of the arrays aa and bb.

The second line of each testcase contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_{n} (1≤ai≤2⋅n1 \le a_i \le 2 \cdot n).

The third line of each testcase contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_{n} (1≤bi≤2⋅n1 \le b_i \le 2 \cdot n).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 表示数组 aa 和 bb 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_{n}(1≤ai≤2⋅n1 \le a_i \le 2 \cdot n)。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1,b_2,\ldots,b_{n}(1≤bi≤2⋅n1 \le b_i \le 2 \cdot n)。

保证所有测试用例中 nn 的总和不超过 10510^5。

输出格式

For each testcase, output the maximum value of min⁡(a1,b1)\min(a_1,b_1) attainable.

对于每个测试用例,输出可达到的 min⁡(a1,b1)\min(a_1,b_1) 的最大值。

输入输出样例

  • 输入#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)\min(1, 2) which is 11.

For the second example, we can do the following sequence of moves:

  • select i=1i = 1 and then:
    • S=2,4,1,3S = {2, 4, 1, 3}, s1=1s_1 = 1, s2=2s_2 = 2, s3=3s_3 = 3, s4=4s_4 = 4
    • a=[2,4,5]→[2,5]a = [\color{red}{2, 4}, 5] \rightarrow [\color{red}{2}, 5]
    • b=[1,3,6]→[3,6]b = [\color{red}{1, 3}, 6] \rightarrow [\color{red}{3}, 6]
  • select i=1i = 1 and then
    • S=2,5,3,6S = {2, 5, 3, 6}, s1=2s_1 = 2, s2=3s_2 = 3, s3=5s_3 = 5, s4=6s_4 = 6
    • a=[2,5]→[3]a = [\color{red}{2, 5}] \rightarrow [\color{red}{3}]
    • b=[3,6]→[5]b = [\color{red}{3, 6}] \rightarrow [\color{red}{5}]

The answer is then min⁡(3,5)\min(3, 5) which is 33, it can be proven that this is optimal.

在第一个例子中,我们无需执行任何操作,因此答案即为 min⁡(1,2)\min(1, 2),也就是 11。

对于第二个例子,我们可以执行以下操作序列:

  • 选择 i=1i = 1,然后:
    • S=2,4,1,3S = {2, 4, 1, 3},s1=1s_1 = 1,s2=2s_2 = 2,s3=3s_3 = 3,s4=4s_4 = 4
    • a=[2,4,5]→[2,5]a = [\color{red}{2, 4}, 5] \rightarrow [\color{red}{2}, 5]
    • b=[1,3,6]→[3,6]b = [\color{red}{1, 3}, 6] \rightarrow [\color{red}{3}, 6]
  • 再次选择 i=1i = 1,然后:
    • S=2,5,3,6S = {2, 5, 3, 6},s1=2s_1 = 2,s2=3s_2 = 3,s3=5s_3 = 5,s4=6s_4 = 6
    • a=[2,5]→[3]a = [\color{red}{2, 5}] \rightarrow [\color{red}{3}]
    • b=[3,6]→[5]b = [\color{red}{3, 6}] \rightarrow [\color{red}{5}]

此时答案为 min⁡(3,5)\min(3, 5),即 33;可以证明该结果是最优的。

输入解题思路,AI测评打分。不知道怎么写?

首页