CF2236E.Friendly Gifts

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Arseniy decided to make his friends Dabir and Egor happy. For this, he decided to give each of them an array of numbers of the same length. An array bb is called good if its elements can be rearranged so that for all i>1i \gt 1 the condition bi−bi−1=1b_i - b_{i - 1} = 1 holds.

Arseniy wants Dabir and Egor to be able to play with these arrays. For this, the following conditions must be satisfied:

  1. Each of the given arrays is good.
  2. If you write one array after the other (in other words, concatenate them), the resulting array is also good.

Arseniy already has an array aa of length nn. He plans to cut both arrays from aa, that is, to choose two non-overlapping subsegments of the same length. Help Arseniy determine the maximum possible length of the resulting arrays.

阿尔塞尼决定让他的朋友达比尔和埃戈尔开心。为此,他决定分别送给他们一个长度相同的数字数组。若数组 bb 的元素可以重新排列,使得对所有 i>1i > 1 均满足 bi−bi−1=1b_i - b_{i-1} = 1,则称该数组 bb 是“好的”。

阿尔塞尼希望达比尔和埃戈尔能用这些数组一起玩耍。为此,需满足以下条件:

  1. 给定的两个数组各自都是“好的”;
  2. 若将其中一个数组接在另一个数组之后(即二者拼接),所得的数组也必须是“好的”。

阿尔塞尼已有一个长度为 nn 的数组 aa。他计划从 aa 中切出这两个数组,即选择两个长度相同且互不重叠的子段。请帮助阿尔塞尼确定所得数组的最大可能长度。

输入格式

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

Then tt test cases follow.

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

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n)(1 \le a_i \le n).

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

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

接下来是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤60001 \le n \le 6000)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \le a_i \le n)。

保证所有测试用例的 nn 值之和不超过 60006000。

输出格式

For each test case, output a single integer — the maximum possible length of the arrays.

对于每个测试用例,输出一个整数——数组可能的最大长度。

输入输出样例

  • 输入#1

    7
    1
    1
    2
    1 2
    3
    2 1 1
    4
    2 1 4 3
    5
    1 2 4 5 3
    6
    3 2 1 6 5 4
    10
    1 1 2 3 4 1 6 5 7 8

    输出#1

    0
    1
    1
    2
    1
    3
    4

说明/提示

In the first sample, it is impossible to select 22 arrays, so the answer is 00.

In the second sample, the maximum length of the selected arrays is 11. Arrays [11] and [22] can be selected.

In the fourth sample, the maximum length of the selected arrays is 22. You can select arrays [2,12, 1] and [4,34, 3].

In the fifth sample, the maximum length of the selected arrays is 11. One way to select arrays is [11] and [22]. Other methods are arrays [22] and [33], [33] and [44], or [44] and [55].

在第一个样例中,无法选择 22 个数组,因此答案为 00。

在第二个样例中,所选数组的最大长度为 11。可以选择数组 [11] 和 [22]。

在第四个样例中,所选数组的最大长度为 22。可以选择数组 [2,12, 1] 和 [4,34, 3]。

在第五个样例中,所选数组的最大长度为 11。一种选择数组的方式是 [11] 和 [22]。其他方式包括数组 [22] 和 [33]、[33] 和 [44],或 [44] 和 [55]。

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

首页