問題一覧 > 通常問題

No.1827 Longest Super Rich Kadomatsu Sequence Subsequence

レベル : / AI推定難易度 : 1900 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 58
作問者 : mai / テスター : 37zigen
お気に入りにしたユーザー ProblemId : 6027 / 自分の提出
問題文最終更新日: 2022-04-26 01:10:12
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 329 (順位表) の他の問題:

Definition

In this problem, a sequence $a_1 \ldots a_n$ of length at least 2 satisfying the following conditions is called a super rich kadomatsu sequence.

  • All elements of the sequence are distinct
  • $\min_{i\in \text{even}}a_i \gt \max_{i\in\text{odd}}a_i$ or $\max_{i\in\text{even}}a_i \lt \min_{i\in\text{odd}}a_i$

The original definition comes from No.122 Trends and Countermeasures: Kadomatsu Sequence (Part 3), but note that it has been extended to sequences of arbitrary length.
Also, this definition differs from that of a kadomatsu sequence sequence. $1,3,2,5,4$ satisfies the conditions of a kadomatsu sequence sequence, but does not satisfy the conditions of a super rich kadomatsu sequence.

Problem Statement

Among the subsequences (not necessarily contiguous) of the permutation $P_1\ldots P_N$ that satisfy the conditions of a super rich kadomatsu sequence, output the length of the longest one.

Input

$T$ test cases are given at once. $(1 \le T \le 10^5)$

$T$
$\text{TestCase}_1$
$\vdots$
$\text{TestCase}_T$

Each test case is given in the following format.

$N$
$P_1$ $\ldots$ $P_N$
  • $3 \le N \le 5\times 10^5$
  • $1 \le P_i \le N$
  • $P_i \neq P_j , (i \neq j)$

When there are multiple test cases, the sum of $N$ does not exceed $5\times 10^5$.

Output

For each test case, output the answer, separated by newlines.

Sample

Sample 1
Input
3
5
1 4 3 2 5
5
2 1 3 5 4
6
1 2 3 4 5 6
Output
4
3
2

In the first test case, 1 4 3 2 5 is given.
For example, 1 4 2 5 is a subsequence of the input and satisfies the conditions of a super rich kadomatsu sequence.
1 4 3 2 5 is a subsequence of the input, but does not satisfy the conditions of a super rich kadomatsu sequence.
Length 4 is the longest, so output 4

$2,1,5,4$ satisfies the conditions of a kadomatsu sequence sequence, but does not satisfy the conditions of a super rich kadomatsu sequence.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。