CF2227D.Palindromex

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Yousef has given you an array aa of 2n2n integers. Every integer x∈[0,n−1]x \in [0, n - 1] appears exactly twice in the array.

Your task is to find a subarray al,al+1,…,ara_l, a_{l + 1}, \dots, a_r that is a palindrome∗^{\text{∗}} such that its mex⁡(al,al+1,…,ar)\operatorname{mex}(a_l, a_{l + 1}, \dots, a_r)†^{\text{†}} is maximized. Output this maximum possible value.

∗^{\text{∗}}A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.

†^{\text{†}}The mex⁡\operatorname{mex} (minimum excludant) of an array of integers is defined as the smallest non-negative integer which does not occur in the array. For example:

  • The mex⁡\operatorname{mex} of [2,2,1][2,2,1] is 00, because 00 does not belong to the array.
  • The mex⁡\operatorname{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⁡\operatorname{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.

优素福给了你一个包含 2n2n 个整数的数组 aa。对于每个整数 x∈[0,n−1]x \in [0, n - 1],它在数组中恰好出现两次。

你的任务是找出一个回文子数组 al,al+1,…,ara_l, a_{l + 1}, \dots, a_r,使得其 mex⁡(al,al+1,…,ar)\operatorname{mex}(a_l, a_{l + 1}, \dots, a_r)†^{\text{†}} 最大化。请输出这个可能的最大值。

∗^{\text{∗}} 回文是指正读与反读都相同的字符串,例如字符串 "z"、"aaa"、"aba"、"abccba" 都是回文,但字符串 "codeforces"、"reality"、"ab" 不是。

†^{\text{†}} 一个整数数组的 mex⁡\operatorname{mex}(最小未出现值)定义为不在该数组中出现的最小非负整数。例如:

  • [2,2,1][2,2,1] 的 mex⁡\operatorname{mex} 是 00,因为 00 不在数组中;
  • [3,1,0,1][3,1,0,1] 的 mex⁡\operatorname{mex} 是 22,因为 00 和 11 在数组中,但 22 不在;
  • [0,3,1,2][0,3,1,2] 的 mex⁡\operatorname{mex} 是 44,因为 00、11、22 和 33 都在数组中,但 44 不在。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5).

The second line of each test case contains 2n2n integers a1,a2,…,a2na_1, a_2, \dots, a_{2n} (0≤ai≤n−10 \le a_i \le n-1). It is guaranteed that every integer in the range [0,n−1][0, n-1] appears exactly twice.

It is guaranteed that the sum of 2n2n over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)。

每个测试用例的第二行包含 2n2n 个整数 a1,a2,…,a2na_1, a_2, \dots, a_{2n}(0≤ai≤n−10 \le a_i \le n-1)。保证区间 [0,n−1][0, n-1] 中的每个整数恰好出现两次。

保证所有测试用例中 2n2n 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the maximum mex⁡\operatorname{mex} of any palindromic subarray.

对于每个测试用例,输出一个整数——任意回文子数组的最大 mex⁡\operatorname{mex} 值。

输入输出样例

  • 输入#1

    6
    4
    1 2 0 3 3 0 2 1
    2
    0 1 0 1
    2
    1 1 0 0
    3
    2 0 2 1 1 0
    4
    0 1 3 0 3 1 2 2
    3
    0 1 2 1 0 2

    输出#1

    4
    2
    1
    1
    2
    3

说明/提示

In the first test case, the only optimal subarray to choose is a[1,8]=[1,2,0,3,3,0,2,1]a[1, 8] = [1, 2, 0, 3, 3, 0, 2, 1], which is palindromic and has a mex⁡\operatorname{mex} of 44.

In the second test case, one of the optimal subarrays to choose is a[2,4]=[1,0,1]a[2, 4] = [1, 0, 1], which is palindromic and has a mex⁡\operatorname{mex} of 22.

In the third test case, we can choose a[3,3]=[0]a[3, 3] = [0], which is palindromic and has a mex⁡\operatorname{mex} of 11. No other palindromic subarray has a mex⁡\operatorname{mex} greater than 11.

在第一个测试用例中,唯一最优的可选子数组是 a[1,8]=[1,2,0,3,3,0,2,1]a[1, 8] = [1, 2, 0, 3, 3, 0, 2, 1],该子数组是回文的,且其 mex⁡\operatorname{mex} 值为 44。

在第二个测试用例中,一个最优的可选子数组是 a[2,4]=[1,0,1]a[2, 4] = [1, 0, 1],该子数组是回文的,且其 mex⁡\operatorname{mex} 值为 22。

在第三个测试用例中,我们可以选择 a[3,3]=[0]a[3, 3] = [0],该子数组是回文的,且其 mex⁡\operatorname{mex} 值为 11。不存在其他回文子数组的 mex⁡\operatorname{mex} 值大于 11。

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

首页