Codeforces Round 1095 (Div. 2)
D
Reserved Reversals
Codeforces
1900
原题
← CE →
翻译中…

You are given an array $a$ consisting of $n$ positive integers.

翻译中…

For any segment of the array starting at position $l$ and ending at position $r$ ($1 \le l \le r \le n$), let $m(l, r)$ denote the minimum value in that segment, and $M(l, r)$ denote the maximum value in that segment. Formally,

翻译中…

$$ \begin{aligned} m(l,r) &= \min(a_l, a_{l+1}, \ldots, a_r),\\ M(l,r) &= \max(a_l, a_{l+1}, \ldots, a_r).\\ \end{aligned} $$

翻译中…

You may perform the following operation any number of times (possibly zero):

翻译中…
  • Select two indices $l$ and $r$ ($1 \le l \le r \le n$) such that $m(l, r) + M(l, r)$ is odd;
  • Reverse the entire segment $a_l, a_{l+1}, \ldots, a_r$. In other words, for every $i$ with $l \le i \le r$, set $a_i := a_{l+r-i}$ simultaneously.
翻译中…

Determine whether you can make the array $a$ non-decreasing$^{\text{∗}}$ by performing a series of operations.

翻译中…

$^{\text{∗}}$An array $[b_1, b_2, \ldots, b_k]$ is considered non-decreasing iff $b_1 \le b_2 \le \ldots \le b_k$.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each testcase contains a single integer $n$ ($1 \le n \le 2\cdot10^5$) — the length of the array $a$.

翻译中…

The second line of each testcase contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of the array.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, print "YES" if you can make the array $a$ non-decreasing, and "NO" otherwise.

翻译中…

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

翻译中…

For the first testcase, the array is already non-decreasing. Hence, output YES.

翻译中…

For the second testcase, let us choose $l = 1$ and $r = 2$. We can see that $\min(2, 1) + \max(2, 1) = 2 + 1 = 3$, which is odd. Thus, after simultaneously assigning $a_i := a_{3-i}$ for all $1 \le i \le 2$, we get $a = [1, 2, 3]$, which is non-decreasing.

翻译中…

Consider the fourth testcase,

翻译中…
  • Operation 1: Choose $l = 1$, $r = 3$. The array becomes $a = [2, 1, 4, 3, 3, 6]$.
  • Operation 2: Choose $l = 3$, $r = 5$. The array becomes $a = [2, 1, 3, 3, 4, 6]$.
  • Operation 3: Choose $l = 1$, $r = 2$. The array becomes $a = [1, 2, 3, 3, 4, 6]$.
翻译中…

For the fifth testcase, note that it is impossible to choose indices $l$ and $r$ satisfying the conditions. Hence, output NO.

翻译中…

样例

Input #1
6
4
1 1 2 3
3
2 1 3
5
5 4 3 2 1
6
4 1 2 3 3 6
5
4 2 4 2 4
6
3 3 1 5 5 2
Output #1
YES
YES
NO
YES
NO
NO
InputCopy OutputCopy
题目信息
题号
2226D
时间限制
2 s
内存限制
256 MB
标签
构造
动态规划
贪心
数学

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