More Cow Photos

시간 제한2초메모리 제한2048 MB

요약
여러 높이로 이루어진 배열에서 좌우 대칭이고 이웃한 값이 서로 다르며 증가하다가 감소하는 가장 긴 부분 수열의 길이를 구한다.
난이도

보통10점 중 6점

유형
그리디, 정렬, 조합론, 구현
정답자
아직 제출이 없습니다

문제

The cows are in a particularly mischievous mood today! All Farmer John wants to do is take a photograph of the cows standing in a line, but they keep moving right before he has a chance to snap the picture.

Specifically, each of FJ's NN cows (1≤N≤105)(1 \le N \le 10^5) has an integer height from 11 to NN. FJ wants to take a picture of the cows standing in line in a very specific ordering. If the cows have heights h_1,…,h_Kh\_1, \dots, h\_K when lined up from left to right, he wants the cow heights to have the following three properties:

  • He wants the cow heights to increase and then decrease. Formally, there must exist an integer ii such that h_1≤⋯≤h_i≥⋯≥h_Kh\_1 \le \dots \le h\_i \ge \dots \ge h\_K.
  • He does not want any cow standing next to another cow with exactly the same height. Formally, h_i≠h_i+1h\_i \neq h\_{i+1} for all 1≤i<K1 \le i < K.
  • He wants the picture to be symmetric. Formally, if i+j=K+1i + j = K+1, then h_i=h_jh\_i = h\_j.

FJ wants the picture to contain as many cows as possible. Specifically, FJ can remove some cows and rearrange the remaining ones. Compute the maximum number of cows FJ can have in the picture satisfying his constraints.

입력

You have to answer multiple test cases.

The first line of input contains a single integer TT (1≤T≤1051 \leq T \leq 10^5) denoting the number of test cases. TT test cases follow.

The first line of every test case contains a single integer NN. The second line of every test case contains NN integers, the heights of the NN cows available. The cow heights will be between 11 and NN.

It is guaranteed the sum of NN over all test cases will not exceed 10610^6.

출력

Output TT lines, the ii'th line containing the answer to the ii'th test case. Each line should be an integer denoting the maximum number of cows FJ can include in the picture.

예제1

  1. 예제 1

    입력
    2
    4
    1 1 2 3
    4
    3 3 2 1
    
    예상 출력
    3
    1