#P2757. [国家集训队] 等差子序列

[国家集训队] 等差子序列

Description

Given a permutation {Ai}\{A_i\} of 11 to NN, determine whether there exist indices

$$1 \le p_1 < p_2 < p_3 < p_4 < p_5 < \cdots < p_{Len} \le N, \quad Len \ge 3,$$

such that Ap1,Ap2,Ap3,,ApLenA_{p_1}, A_{p_2}, A_{p_3}, \cdots, A_{p_{Len}} form an arithmetic progression.

Input Format

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

Then follow TT test cases. For each test case, the first line contains an integer NN, and the second line contains a permutation of 11 to NN, with numbers separated by single spaces.

Output Format

For each test case, output a single line with Y if there exists an arithmetic subsequence, otherwise output N.

2
3
1 3 2
3
3 2 1
N
Y

Hint

For the first 55 test points, 1N5×1051 \leq N \leq 5 \times 10^5, T5T \leq 5, time limit 55 s.

For the remaining 2121 test points, 1N100001 \leq N \leq 10000, T7T \leq 7, time limit 22 s.

Translated by ChatGPT 5