Up and Down

면접 대비

시간 제한3초메모리 제한1024 MB

요약
엄격히 증가하다가 엄격히 감소하는 부분수열 중에서 꼭짓점을 공유하고 양쪽 길이가 각각 2 이상인 가장 긴 것을 찾는다.
난이도

보통10점 중 6점

유형
동적 계획법, 이분 탐색, 배열, 그리디
정답자
아직 제출이 없습니다

문제

Given a sequence of integers with length NN, find the maximum length of a subsequence that strictly increases and then strictly decreases; the last position of strictly increasing must coincide with the first position of strictly decreasing. A subsequence can be obtained from a sequence by removing some or no elements from the sequence without changing the order of the remaining elements.

For this problem, the length of the strictly increasing subsequence and the length of the strictly decreasing subsequence should be at least 2. In particular, an empty subsequence or a subsequence of length 1 DOES NOT strictly increase nor does it strictly decrease. See the examples below for further illustration.

Some examples:

  • 7 neither strictly increases nor strictly decreases;
  • 1 1 1 neither strictly increases nor strictly decreases;
  • 1 1 2 neither strictly increases nor strictly decreases;
  • 1 2 7 strictly increases; it does not strictly decrease;
  • 3 2 1 strictly decreases; it does not strictly increase;
  • 8 9 3 strictly increases and then strictly decreases; this is the type of subsequence your solution needs to find.

입력

A positive integer TT (1≤T≤5⋅104)(1 \le T \le 5 \cdot 10^4), denoting a number of test cases, is written in the first line.

Each test case consists of two lines. The first line contains a positive integer NN (3≤N≤2⋅105)(3 \le N \le 2 \cdot 10^5), denoting a number of integers in the list. The second line contains NN integers a_ia\_i, each in the range of a signed 32-bit integer (−231≤a_i≤231−1)(-2^{31} \le a\_i \le 2^{31}-1).

For each input file, the total number of integers in the lists across all test cases will not exceed 5⋅1055 \cdot 10^5.

출력

For each test case, output a single non-negative integer: the maximum length among all subsequences that strictly increase and, then, strictly decrease. For example, in Sample Input 3, the subsequence 1 2 4 5 2 1 is the maximal length "up-and-down" subsequence for that input, so the output should be 66.

예제3

  1. 예제 1

    입력
    1
    7
    1 2 3 4 5 6 7 
    
    예상 출력
    0
    
  2. 예제 2

    입력
    1
    6
    1 2 3 3 2 1 
    
    예상 출력
    5
    
  3. 예제 3

    입력
    1
    8
    1 11 2 10 4 5 2 1 
    
    예상 출력
    6