CF1335E2.Three Blocks Palindrome (hard version)

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The only difference between easy and hard versions is constraints.

You are given a sequence aa consisting of nn positive integers.

Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are aa and bb, aa can be equal bb) and is as follows: [a,a,…,a⏟x,b,b,…,b⏟y,a,a,…,a⏟x][\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]. There x,yx, y are integers greater than or equal to 00. For example, sequences [][], [2][2], [1,1][1, 1], [1,2,1][1, 2, 1], [1,2,2,1][1, 2, 2, 1] and [1,1,2,1,1][1, 1, 2, 1, 1] are three block palindromes but [1,2,3,2,1][1, 2, 3, 2, 1], [1,2,1,2,1][1, 2, 1, 2, 1] and [1,2][1, 2] are not.

Your task is to choose the maximum by length subsequence of aa that is a three blocks palindrome.

You have to answer tt independent test cases.

Recall that the sequence tt is a a subsequence of the sequence ss if tt can be derived from ss by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1,2,1,3,1,2,1]s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1,1,1,1][1, 1, 1, 1], [3][3] and [1,2,1,3,1,2,1][1, 2, 1, 3, 1, 2, 1], but not [3,2,3][3, 2, 3] and [1,1,1,1,2][1, 1, 1, 1, 2].

简单版本与困难版本的唯一区别在于约束条件。

给你一个由 nn 个正整数组成的序列 aa。

我们定义三段回文序列(three blocks palindrome)为:至多包含两种不同元素(设这两种元素为 aa 和 bb,其中 aa 可以等于 bb)且形如

[a,a,…,a⏟x,b,b,…,b⏟y,a,a,…,a⏟x][\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]

的序列。其中 x,yx, y 是大于等于 00 的整数。例如,以下序列均为三段回文序列:[][]、[2][2]、[1,1][1, 1]、[1,2,1][1, 2, 1]、[1,2,2,1][1, 2, 2, 1] 和 [1,1,2,1,1][1, 1, 2, 1, 1];而以下序列不是三段回文序列:[1,2,3,2,1][1, 2, 3, 2, 1]、[1,2,1,2,1][1, 2, 1, 2, 1] 和 [1,2][1, 2]。

你的任务是:在序列 aa 中选出一个长度最大的子序列,使其构成一个三段回文序列。

你需要回答 tt 个相互独立的测试用例。

注意:若序列 tt 可通过从序列 ss 中删除零个或多个元素(不改变剩余元素的相对顺序)得到,则称 tt 是 ss 的一个子序列。例如,若 s=[1,2,1,3,1,2,1]s=[1, 2, 1, 3, 1, 2, 1],则可能的子序列包括:[1,1,1,1][1, 1, 1, 1]、[3][3] 和 [1,2,1,3,1,2,1][1, 2, 1, 3, 1, 2, 1];但 [3,2,3][3, 2, 3] 和 [1,1,1,1,2][1, 1, 1, 1, 2] 不是其子序列。

输入格式

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

The first line of the test case contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of aa. The second line of the test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤2001 \le a_i \le 200), where aia_i is the ii-th element of aa. Note that the maximum value of aia_i can be up to 200200.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5 (∑n≤2⋅105\sum n \le 2 \cdot 10^5).

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示数组 aa 的长度。每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤2001 \le a_i \le 200),其中 aia_i 是数组 aa 的第 ii 个元素。注意,aia_i 的最大值可达 200200。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5(即 ∑n≤2⋅105\sum n \le 2 \cdot 10^5)。

输出格式

For each test case, print the answer — the maximum possible length of some subsequence of aa that is a three blocks palindrome.

对于每个测试用例,输出答案——即数组 aa 的某个子序列所能构成的三段回文的最大可能长度。

输入输出样例

  • 输入#1

    6
    8
    1 1 2 2 3 2 1 1
    3
    1 3 3
    4
    1 10 10 1
    1
    26
    2
    2 1
    3
    1 1 1

    输出#1

    7
    2
    4
    1
    1
    3

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

首页