A15965 | Ian and Array Sorting
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
To thank Ian, Mary gifted an array $a$ of length $n$ to Ian. To make himself look smart, he wants to make the array in non-decreasing order by doing the following finitely many times: he chooses two adjacent elements $a_i$ and $a_{i+1}$ ( $1\le i\le n-1$ ), and increases both of them by $1$ or decreases both of them by $1$ . Note that, the elements of the array can become negative.
As a smart person, you notice that, there are some arrays such that Ian cannot make it become non-decreasing order! Therefore, you decide to write a program to determine if it is possible to make the array in non-decreasing order.
As a smart person, you notice that, there are some arrays such that Ian cannot make it become non-decreasing order! Therefore, you decide to write a program to determine if it is possible to make the array in non-decreasing order.
输入格式
The first line contains a single integer $t$ ( $1 \leq t \leq 10^4$ ) — the number of test cases. The description of test cases follows.
The first line of each test case consists of a single integer $n$ ( $2\le n\le 3\cdot10^5$ ) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ( $1\le a_i\le 10^9$ ) — the elements of the array $a$ .
It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$ .
The first line of each test case consists of a single integer $n$ ( $2\le n\le 3\cdot10^5$ ) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ( $1\le a_i\le 10^9$ ) — the elements of the array $a$ .
It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$ .
输出格式
For each test case, output "YES" if there exists a sequence of operations which make the array non-decreasing, else output "NO".
You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).
You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).
输入输出样例
输入 #1
5 3 1 3 2 2 2 1 4 1 3 5 7 4 2 1 4 3 5 5 4 3 2 1
输出 #1
YES NO YES NO YES
For the first test case, we can increase $a_2$ and $a_3$ both by $1$ . The array is now $[1, 4, 3]$ .
Then we can decrease $a_1$ and $a_2$ both by $1$ . The array is now $[0, 3, 3]$ , which is sorted in non-decreasing order. So the answer is "YES".
For the second test case, no matter how Ian perform the operations, $a_1$ will always be larger than $a_2$ . So the answer is "NO" and Ian cannot pretend to be smart.
For the third test case, the array is already in non-decreasing order, so Ian does not need to do anything.
Then we can decrease $a_1$ and $a_2$ both by $1$ . The array is now $[0, 3, 3]$ , which is sorted in non-decreasing order. So the answer is "YES".
For the second test case, no matter how Ian perform the operations, $a_1$ will always be larger than $a_2$ . So the answer is "NO" and Ian cannot pretend to be smart.
For the third test case, the array is already in non-decreasing order, so Ian does not need to do anything.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted