CF2226D.Reserved Reversals
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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≤l≤r≤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≤l≤r≤n) such that m(l,r)+M(l,r) is odd;
- Reverse the entire segment al,al+1,…,ar. In other words, for every i with l≤i≤r, set ai:=al+r−i simultaneously.
Determine whether you can make the array a non-decreasing∗ by performing a series of operations.
∗An array [b1,b2,…,bk] is considered non-decreasing iff b1≤b2≤…≤bk.
给你一个由 n 个正整数组成的数组 a。
对于数组中任意一个起始于位置 l、终止于位置 r 的子段(其中 1≤l≤r≤n),记 m(l,r) 为该子段中的最小值,M(l,r) 为该子段中的最大值。形式化地,
m(l,r)M(l,r)=min(al,al+1,…,ar),=max(al,al+1,…,ar).
你可以执行以下操作任意多次(包括零次):
- 选择两个下标 l 和 r(满足 1≤l≤r≤n),使得 m(l,r)+M(l,r) 为奇数;
- 将整个子段 al,al+1,…,ar 翻转。换言之,对每个满足 l≤i≤r 的 i,同时令 ai:=al+r−i。
判断是否可以通过一系列上述操作,使数组 a 变为非递减序列∗。
∗ 数组 [b1,b2,…,bk] 被称为非递减的,当且仅当 b1≤b2≤…≤bk。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each testcase contains a single integer n (1≤n≤2⋅105) — the length of the array a.
The second line of each testcase contains n integers a1,a2,…,an (1≤ai≤n) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n)—— 数组的元素。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
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.
对于每个测试用例,如果可以使数组 a 变为非递减的,则输出 "YES";否则输出 "NO"。
你可以以任意大小写形式输出答案(大写或小写均可)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答。
输入输出样例
输入#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
输出#1
YES YES NO YES NO NO
说明/提示
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 ai:=a3−i for all 1≤i≤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.
对于第一个测试用例,数组已经是非递减的,因此输出 YES。
对于第二个测试用例,我们选择 l=1 和 r=2。可以验证 min(2,1)+max(2,1)=2+1=3,该值为奇数。因此,在对所有 1≤i≤2 同时执行赋值操作 ai:=a3−i 后,得到 a=[1,2,3],该数组是非递减的。
考虑第四个测试用例:
- 操作 1:选择 l=1,r=3,数组变为 a=[2,1,4,3,3,6]。
- 操作 2:选择 l=3,r=5,数组变为 a=[2,1,3,3,4,6]。
- 操作 3:选择 l=1,r=2,数组变为 a=[1,2,3,3,4,6]。
对于第五个测试用例,注意不存在满足条件的下标 l 和 r。因此输出 NO。
输入解题思路,AI测评打分。不知道怎么写?