atcoder#AGC058C. [AGC058C] Planar Tree

[AGC058C] Planar Tree

Score : 900900 points

Problem Statement

There are NN vertices on a circumference. The vertices are numbered 11 to NN in clockwise order, and Vertex ii has an integer AiA_i written on it, where AiA_i is 11, 22, 33, or 44. Here, AA contains each of 11, 22, 33, and 44 at least once.

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

  • If Vertices xx and yy are directly connected by an edge, AxAy=1|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 TT test cases for each input file.

Constraints

  • 1T750001 \leq T \leq 75000
  • 4N3000004 \leq N \leq 300000
  • 1Ai41 \leq A_i \leq 4
  • AA contains each of 11, 22, 33, and 44 at least once.
  • The sum of NN in one input file does not exceed 300000300000.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

TT

case1case_1

case2case_2

\vdots

caseTcase_T

Each case is in the following format:

NN

A1A_1 A2A_2 \cdots ANA_N

Output

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

3
4
1 2 3 4
4
1 3 4 2
4
1 4 2 3
Yes
Yes
No
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
Yes
Yes
No

The first test case corresponds to the figure in the Problem Statement.