CF1699D.Almost Triple Deletions
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an integer n and an array a1,a2,…,an.
In one operation, you can choose an index i (1≤i<n) for which ai=ai+1 and delete both ai and ai+1 from the array. After deleting ai and ai+1, the remaining parts of the array are concatenated.
For example, if a=[1,4,3,3,6,2], then after performing an operation with i=2, the resulting array will be [1,3,6,2].
What is the maximum possible length of an array of equal elements obtainable from a by performing several (perhaps none) of the aforementioned operations?
给你一个整数 n 和一个数组 a1,a2,…,an。
在一次操作中,你可以选择一个下标 i(满足 1≤i<n)使得 ai=ai+1,然后将 ai 和 ai+1 同时从数组中删除。删除后,数组剩余的两部分会直接拼接起来。
例如,若 a=[1,4,3,3,6,2],则对 i=2 执行一次操作后,得到的新数组为 [1,3,6,2]。
通过执行若干次(可能为零次)上述操作,你能得到的所有元素均相等的数组的最大可能长度是多少?
输入格式
Each test contains multiple test cases. The first line of input contains one integer t (1≤t≤1000) — the number of test cases. The following lines contain the descriptions of the test cases.
The first line of each test case contains a single integer n (1≤n≤5000) — the length of array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the elements of array a.
It is guaranteed that the sum of n across all test cases does not exceed 10000.
每个测试包含多个测试用例。输入的第一行包含一个整数 t(1≤t≤1000),表示测试用例的数量。接下来的行描述各个测试用例。
每个测试用例的第一行包含一个整数 n(1≤n≤5000),表示数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n),表示数组 a 的元素。
保证所有测试用例中 n 的总和不超过 10000。
输出格式
For each testcase, print a single integer, the maximum possible length of an array of equal elements obtainable from a by performing a sequence of operations.
对于每个测试用例,输出一个整数,表示通过对数组 a 执行一系列操作所能得到的、由相等元素构成的数组的最大可能长度。
输入输出样例
输入#1
5 7 1 2 3 2 1 3 3 1 1 6 1 1 1 2 2 2 8 1 1 2 2 3 3 1 1 12 1 5 2 3 3 3 4 4 4 4 3 3
输出#1
3 1 0 4 2
说明/提示
For the first testcase, an optimal sequence of operations would be: [1,2,3,2,1,3,3]→[3,2,1,3,3]→[3,3,3].
For the second testcase, all elements in the array are already equal.
For the third testcase, the only possible sequence of operations is: [1,1,1,2,2,2]→[1,1,2,2]→[1,2]→[]. Note that, according to the statement, the elements deleted at each step must be different.
For the fourth testcase, the optimal sequence of operations is: [1,1,2,2,3,3,1,1]→[1,1,2,3,1,1]→[1,1,1,1].
For the fifth testcase, one possible reachable array of two equal elements is [4,4].
对于第一个测试用例,一种最优的操作序列如下:[1,2,3,2,1,3,3]→[3,2,1,3,3]→[3,3,3]。
对于第二个测试用例,数组中所有元素已经相等。
对于第三个测试用例,唯一可能的操作序列是:[1,1,1,2,2,2]→[1,1,2,2]→[1,2]→[]。注意,根据题面要求,每一步删除的元素必须互不相同。
对于第四个测试用例,最优的操作序列是:[1,1,2,2,3,3,1,1]→[1,1,2,3,1,1]→[1,1,1,1]。
对于第五个测试用例,一个可达的、包含两个相等元素的数组是 [4,4]。
输入解题思路,AI测评打分。不知道怎么写?