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 a consisting of n positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [xa,a,…,a,yb,b,…,b,xa,a,…,a]. There x,y are integers greater than or equal to 0. For example, sequences [], [2], [1,1], [1,2,1], [1,2,2,1] and [1,1,2,1,1] are three block palindromes but [1,2,3,2,1], [1,2,1,2,1] and [1,2] are not.
Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome.
You have to answer t independent test cases.
Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s 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], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].
简单版本与困难版本的唯一区别在于约束条件。
给你一个由 n 个正整数组成的序列 a。
我们定义三段回文序列(three blocks palindrome)为:至多包含两种不同元素(设这两种元素为 a 和 b,其中 a 可以等于 b)且形如
[xa,a,…,a,yb,b,…,b,xa,a,…,a]
的序列。其中 x,y 是大于等于 0 的整数。例如,以下序列均为三段回文序列:[]、[2]、[1,1]、[1,2,1]、[1,2,2,1] 和 [1,1,2,1,1];而以下序列不是三段回文序列:[1,2,3,2,1]、[1,2,1,2,1] 和 [1,2]。
你的任务是:在序列 a 中选出一个长度最大的子序列,使其构成一个三段回文序列。
你需要回答 t 个相互独立的测试用例。
注意:若序列 t 可通过从序列 s 中删除零个或多个元素(不改变剩余元素的相对顺序)得到,则称 t 是 s 的一个子序列。例如,若 s=[1,2,1,3,1,2,1],则可能的子序列包括:[1,1,1,1]、[3] 和 [1,2,1,3,1,2,1];但 [3,2,3] 和 [1,1,1,1,2] 不是其子序列。
输入格式
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2⋅105) — the length of a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤200), where ai is the i-th element of a. Note that the maximum value of ai can be up to 200.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).
输入的第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是 t 个测试用例。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105),表示数组 a 的长度。每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤200),其中 ai 是数组 a 的第 i 个元素。注意,ai 的最大值可达 200。
保证所有测试用例的 n 之和不超过 2⋅105(即 ∑n≤2⋅105)。
输出格式
For each test case, print the answer — the maximum possible length of some subsequence of a that is a three blocks palindrome.
对于每个测试用例,输出答案——即数组 a 的某个子序列所能构成的三段回文的最大可能长度。
输入输出样例
输入#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测评打分。不知道怎么写?