Codeforces Round 987 (Div. 2)
B
Penchick and Satay Sticks
Codeforces
900
原题
← AC →
翻译中…

Penchick and his friend Kohane are touring Indonesia, and their next stop is in Surabaya!

翻译中…

In the bustling food stalls of Surabaya, Kohane bought $n$ satay sticks and arranged them in a line, with the $i$-th satay stick having length $p_i$. It is given that $p$ is a permutation$^{\text{∗}}$ of length $n$.

翻译中…

Penchick wants to sort the satay sticks in increasing order of length, so that $p_i=i$ for each $1\le i\le n$. For fun, they created a rule: they can only swap neighboring satay sticks whose lengths differ by exactly $1$. Formally, they can perform the following operation any number of times (including zero):

翻译中…
  • Select an index $i$ ($1\le i\le n-1$) such that $|p_{i+1}-p_i|=1$;
  • Swap $p_i$ and $p_{i+1}$.
翻译中…

Determine whether it is possible to sort the permutation $p$, thus the satay sticks, by performing the above operation.

翻译中…

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2\cdot 10^5$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot 10^5$) — the number of satay sticks.

翻译中…

The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$) — the permutation $p$ representing the length of the satay sticks.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output "YES" if it is possible to sort permutation $p$ by performing the operation. Otherwise, output "NO".

翻译中…

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

翻译中…

Note

翻译中…

In the first test case, we can sort permutation $p = [2, 1, 3, 4]$ by performing an operation on index $1$ ($|p_2 - p_1| = |1 - 2| = 1$), resulting in $p = [1, 2, 3, 4]$.

翻译中…

In the second test case, it can be proven that it is impossible to sort permutation $p = [4, 2, 3, 1]$ by performing the operation. Here is an example of a sequence of operations that can be performed on the permutation:

翻译中…
  • Select $i = 2$ ($|p_3 - p_2| = |3 - 2| = 1$). This results in $p = [4, 3, 2, 1]$.
  • Select $i = 1$ ($|p_2 - p_1| = |3 - 4| = 1$). This results in $p = [3, 4, 2, 1]$.
  • Select $i = 3$ ($|p_4 - p_3| = |1 - 2| = 1$). This results in $p = [3, 4, 1, 2]$.
翻译中…

Unfortunately, permutation $p$ remains unsorted after performing the operations.

翻译中…

样例

Input #1
2
4
2 1 3 4
4
4 2 3 1
Output #1
YES
NO
InputCopy OutputCopy
题目信息
题号
2031B
时间限制
1.5 s
内存限制
256 MB
标签
暴力
贪心
排序

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