#P11013. 「ALFR Round 4」C 粉碎

「ALFR Round 4」C 粉碎

Problem Description

Binbao is playing cards. At first, he has nn cards, and the value of the ii-th card is AiA_i.

Binbao will repeatedly perform the following operation for nn rounds. The operation in round ii is:

  1. Binbao needs to choose to place the ii-th card on the leftmost side or the rightmost side of the pile.
  2. If there exist two cards in the pile with the same value, then Binbao will immediately take all cards between these two cards out of the pile and throw them into the shredder (including these two cards themselves).

The insertion operation is always performed before the shattering operation.

The pile is initially empty.

You need to tell Binbao the maximum number of cards he can shatter.

Input Format

The first line contains an integer TT, indicating the number of test cases.

For each test case:

The first line contains an integer nn.

The second line contains nn numbers representing A1,A2,A3,,AnA_1,A_2,A_3,\cdots,A_n.

Output Format

For each test case, output the maximum number of cards Binbao can shatter.

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

Hint

Sample Explanation

Initial pile: {}\{\}

Put in 11: {1}\{1\};

Put in 33: {1,3}\{1,3\};

Put in 33: {3,1,3}\{3,1,3\}, then shatter: {}\{\};

Put in 11: {1}\{1\};

Put in 22: {1,2}\{1,2\};

Put in 11: {1,2,1}\{1,2,1\}, then shatter: {}\{\};

Put in 22: {2}\{2\};

Put in 22: {2,2}\{2,2\}, then shatter: {}\{\};

All cards are shattered.

Constraints

Subtask Score Constraints
00 2020 n20n\le 20
11 T=1,n=103T=1,n=10^3 and AiA_i is randomly generated within [1,n][1,n]
22 4040 n103n\le10^3
33 2020 -

For 100%100\% of the testdata, 1T51\le T\le5, 1n5×1051\le n\le5\times10^5, 1Ain1\le A_i\le n.

Translated by ChatGPT 5