CF1870E.Another MEX Problem

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array of integers aa of size nn. You can choose a set of non-overlapping subarrays of the given array (note that some elements may be not included in any subarray, this is allowed). For each selected subarray, calculate the MEX of its elements, and then calculate the bitwise XOR of all the obtained MEX values. What is the maximum bitwise XOR that can be obtained?

The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

  • The MEX of [2,2,1][2,2,1] is 00, because 00 does not belong to the array.
  • The MEX of [3,1,0,1][3,1,0,1] is 22, because 00 and 11 belong to the array, but 22 does not.
  • The MEX of [0,3,1,2][0,3,1,2] is 44, because 00, 11, 22 and 33 belong to the array, but 44 does not.

给你一个长度为 nn 的整数数组 aa。你可以从中选择若干个互不重叠的子数组(注意:某些元素可能未被包含在任何子数组中,这是允许的)。对每个选定的子数组,计算其元素的 MEX(最小缺失值),然后计算所有得到的 MEX 值的按位异或。你能获得的最大按位异或值是多少?

数组的 MEX(minimum excluded,最小排除值)是指不属于该数组的最小非负整数。例如:

  • [2,2,1][2,2,1] 的 MEX 是 00,因为 00 不在数组中。
  • [3,1,0,1][3,1,0,1] 的 MEX 是 22,因为 00 和 11 在数组中,但 22 不在。
  • [0,3,1,2][0,3,1,2] 的 MEX 是 44,因为 00、11、22 和 33 都在数组中,但 44 不在。

输入格式

The first line contains an integer tt (1≤t≤50001 \leq t \leq 5000) — the number of test cases. This is followed by the description of the test cases.

The first line of each test case contains an integer nn (1≤n≤50001 \leq n \leq 5000) — the size of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤n0 \leq a_i \leq n) — the array aa.

It is guaranteed that the sum of all nn values across all test cases does not exceed 50005000.

第一行包含一个整数 tt(1≤t≤50001 \leq t \leq 5000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤50001 \leq n \leq 5000),表示数组 aa 的大小。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤n0 \leq a_i \leq n),即数组 aa。

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

输出格式

For each test case, output a single number — the maximum possible XOR of the MEX values of the selected subarrays.

对于每个测试用例,输出一个整数——所选子数组的 MEX 值所能达到的最大异或(XOR)结果。

输入输出样例

  • 输入#1

    4
    2
    1 0
    10
    1 2 0 7 1 2 0 2 4 3
    10
    2 1 0 7 1 2 0 2 4 3
    3
    1 2 1

    输出#1

    2
    6
    7
    0

说明/提示

In the first test case, the maximum XOR is 22 if we take the entire array, MEX⁡([1,0])=2\operatorname{MEX}([1, 0]) = 2.

In the second test case, the maximum XOR is 66 if we partition the array into segments [1,2,0][1, 2, 0] and [7,1,2,0,2,4,3][7, 1, 2, 0, 2, 4, 3]:

  • MEX⁡([1,2,0])=3\operatorname{MEX}([1, 2, 0]) = 3,
  • MEX⁡([7,1,2,0,2,4,3])=5\operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5,

therefore, the XOR is 5⊕3=65 \oplus 3=6.

In the third test case, the maximum XOR is 77 if we partition the array into segments [1,0][1, 0] and [7,1,2,0,2,4,3][7, 1, 2, 0, 2, 4, 3]:

  • MEX⁡([1,0])=2\operatorname{MEX}([1, 0]) = 2,
  • MEX⁡([7,1,2,0,2,4,3])=5\operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5,

therefore, the XOR is 5⊕2=75 \oplus 2 = 7.

在第一个测试用例中,若取整个数组,则最大异或值为 22,即 MEX⁡([1,0])=2\operatorname{MEX}([1, 0]) = 2。

在第二个测试用例中,若将数组划分为子段 [1,2,0][1, 2, 0] 和 [7,1,2,0,2,4,3][7, 1, 2, 0, 2, 4, 3],则最大异或值为 66:

  • MEX⁡([1,2,0])=3\operatorname{MEX}([1, 2, 0]) = 3,
  • MEX⁡([7,1,2,0,2,4,3])=5\operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5,

因此,异或值为 5⊕3=65 \oplus 3 = 6。

在第三个测试用例中,若将数组划分为子段 [1,0][1, 0] 和 [7,1,2,0,2,4,3][7, 1, 2, 0, 2, 4, 3],则最大异或值为 77:

  • MEX⁡([1,0])=2\operatorname{MEX}([1, 0]) = 2,
  • MEX⁡([7,1,2,0,2,4,3])=5\operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5,

因此,异或值为 5⊕2=75 \oplus 2 = 7。

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

首页