Codeforces Round #634 (Div. 3) E2.Three Blocks Palindrome (hard version)

Codeforces Round #634 (Div. 3) E2.Three Blocks Palindrome (hard version)

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: [ a , a , … , a ⏟ x , b , b , … , b ⏟ y , a , a , … , a ⏟ x \underbrace{a,a,…,a}_x,\underbrace{b,b,…,b}_y,\underbrace{a,a,…,a}_x x a,a,,a,y b,b,,b,x a,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].

Input

The first line of the input contains one integer t (1≤t≤1e4) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2e5) — 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 2e5 (∑n≤2e5).

Output

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

Example

input

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

output

7
2
4
1
1
3

简单困难就是数据量不同,这边直接挂一个困难的代码,我的方法都一样~
v e c t o r vector vector 记录每个数出现的位置,我们考虑固定两端的数,求中间出现的数的数量的最大值,可以用二分快速求得:
比如下面的一组数(下标从1开始):

1 1 2 2 3 2 1 1

第一对 1 1 1 的位置, l = 1 , r = 8 l=1,r=8 l=1,r=8 u p p e r _ b o u n d upper\_bound upper_bound 求得大于 l l l 2 2 2 的下标为0,大于 r r r 2 2 2 的下标为3(注意找不到返回end()),所以 a n s = 2 ∗ 1 + 3 − 0 = 5 ans=2*1+3-0=5 ans=21+30=5
第二对 1 1 1 的位置, l = 2 , r = 7 l=2,r=7 l=2,r=7 u p p e r _ b o u n d upper\_bound upper_bound 求得大于 l l l 2 2 2 的下标为0,大于 r r r 2 2 2 的下标为3(注意找不到返回end()),所以 a n s = 2 ∗ 2 + 3 − 0 = 7 ans=2*2+3-0=7 ans=22+30=7
⋮ \vdots
以此类推
每找一对就更新一下答案即可,AC代码如下:

#include
using namespace std;
typedef long long ll;

int main(){
     
    int n,k,t;
    scanf("%d",&t);
    while(t--){
     
        vector<int>v[205];
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
     
            scanf("%d",&k);
            v[k].push_back(i);
        }
        int ans=0;
        for(int i=1;i<=200;i++){
     
            int num=v[i].size();
            if(num==0) continue;
            ans=max(ans,num);
            for(int j=0;j<v[i].size()/2;j++){
     
                int l=v[i][j],r=v[i][num-j-1];
                for(int k=1;k<=200;k++){
     
                    if(k==i) continue;
                    int pos1=upper_bound(v[k].begin(),v[k].end(),l)-v[k].begin();
                    int pos2=upper_bound(v[k].begin(),v[k].end(),r)-v[k].begin();
                    ans=max(ans,(j+1)*2+pos2-pos1);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(思维,二分,Codeforces)