atcoder#AGC058E. [AGC058E] Nearer Permutation

[AGC058E] Nearer Permutation

Score : 14001400 points

Problem Statement

In this problem, a "permutation" always means a permutation of (1,2,,N)(1,2,\cdots,N).

For two permutations pp and qq, let us define the distance between them, d(p,q)d(p,q), as follows.

  • Consider making pp equal qq by repeatedly swapping two adjacent terms in pp. Let d(p,q)d(p,q) be the minimum number of swaps required here.

Additionally, for a permutation xx, let us define another permutation f(x)f(x) as follows.

  • Let y=(1,2,,N)y=(1,2,\cdots,N). Consider permutations zz such that d(x,z)d(y,z)d(x,z) \leq d(y,z). Let f(x)f(x) be the lexicographically smallest of those permutations.

For example, when x=(2,3,1)x=(2,3,1), the permutations zz such that d(x,z)d(y,z)d(x,z) \leq d(y,z) are z=(2,1,3),(2,3,1),(3,1,2),(3,2,1)z=(2,1,3),(2,3,1),(3,1,2),(3,2,1). The lexicographically smallest of them is (2,1,3)(2,1,3), so we have f(x)=(2,1,3)f(x)=(2,1,3).

You are given a permutation A=(A1,A2,,AN)A=(A_1,A_2,\cdots,A_N). Determine whether there is a permutation xx such that f(x)=Af(x)=A.

Solve TT test cases for each input file.

What is lexicographical order on sequences?

The algorithm described here determines the lexicographical order between distinct sequences SS and TT.

Below, let SiS_i denote the ii-th element of SS. Additionally, let S<TS \lt T and S>TS \gt T mean "SS is lexicographical smaller than TT" and "SS is lexicographical larger than TT," respectively.

  1. Let LL be the length of the shorter of SS and TT. For each i=1,2,,Li=1,2,\dots,L, let us check whether SiS_i and TiT_i are equal.
  2. If there is ii such that SiTiS_i \neq T_i, let jj be the smallest such ii, and compare SjS_j and TjT_j. If SjS_j is smaller than TjT_j (as a number), we conclude S<TS \lt T and exit; if SjS_j is larger than TjT_j, we conclude S>TS \gt T and exit.
  3. If there is no ii such that SiTiS_i \neq T_i, compare the lengths of SS and TT. If SS is shorter than TT, we conclude S<TS \lt T and exit; if SS is longer than TT, we conclude S>TS \gt T and exit.

Constraints

  • 1T1500001 \leq T \leq 150000
  • 2N3000002 \leq N \leq 300000
  • (A1,A2,,AN)(A_1,A_2,\cdots,A_N) is a permutation of (1,2,,N)(1,2,\cdots,N).
  • 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 there is a permutation xx such that f(x)=Af(x)=A, and No otherwise.

2
2
1 2
2
2 1
Yes
Yes

For instance, when A=(2,1)A=(2,1), one can let x=(2,1)x=(2,1) to satisfy f(x)=Af(x)=A.

6
3
1 2 3
3
1 3 2
3
2 1 3
3
2 3 1
3
3 1 2
3
3 2 1
Yes
Yes
Yes
Yes
No
No

For instance, when A=(2,3,1)A=(2,3,1), one can let x=(3,2,1)x=(3,2,1) to satisfy f(x)=Af(x)=A.

24
4
1 2 3 4
4
1 2 4 3
4
1 3 2 4
4
1 3 4 2
4
1 4 2 3
4
1 4 3 2
4
2 1 3 4
4
2 1 4 3
4
2 3 1 4
4
2 3 4 1
4
2 4 1 3
4
2 4 3 1
4
3 1 2 4
4
3 1 4 2
4
3 2 1 4
4
3 2 4 1
4
3 4 1 2
4
3 4 2 1
4
4 1 2 3
4
4 1 3 2
4
4 2 1 3
4
4 2 3 1
4
4 3 1 2
4
4 3 2 1
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No