AtCoder Grand Contest 058
C
Planar Tree
AtCoder
⌀ 2991
原题
← BD →
翻译中…

Problem Statement

翻译中…

There are $N$ vertices on a circumference. The vertices are numbered $1$ to $N$ in clockwise order, and Vertex $i$ has an integer $A_i$ written on it, where $A_i$ is $1$, $2$, $3$, or $4$. Here, $A$ contains each of $1$, $2$, $3$, and $4$ at least once.

翻译中…

Consider making a tree by adding $N-1$ edges connecting these $N$ vertices. Here, the following conditions must be satisfied.

翻译中…
  • If Vertices $x$ and $y$ are directly connected by an edge, $|A_x-A_y|=1$.
  • When the edges are drawn as segments, no two of them intersect except at an endpoint.
翻译中…

For instance, the figure below shows a tree that satisfies the conditions:

翻译中…

figure1

翻译中…

Determine whether it is possible to construct a tree that satisfies the conditions.

翻译中…

Solve $T$ test cases for each input file.

翻译中…

Constraints

翻译中…
  • $1 \leq T \leq 75000$
  • $4 \leq N \leq 300000$
  • $1 \leq A_i \leq 4$
  • $A$ contains each of $1$, $2$, $3$, and $4$ at least once.
  • The sum of $N$ in one input file does not exceed $300000$.
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

T
case_1
case_2
\vdots
case_T

翻译中…

Each case is in the following format:

翻译中…

N
A_1 A_2 \cdots A_N

翻译中…

Output

翻译中…

For each case, print Yes if it is possible to construct a tree that satisfies the conditions, and No otherwise.

翻译中…

样例

Input #1
3
4
1 2 3 4
4
1 3 4 2
4
1 4 2 3
Output #1
Yes
Yes
No
Input #2
3
8
4 2 3 4 1 2 2 1
8
3 2 2 3 1 3 3 4
8
2 3 3 2 1 4 1 4
Output #2
Yes
Yes
No
The first test case corresponds to the figure in the Problem Statement.
题目信息
题号
agc058_c
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。