Codeforces div2 C - Ian and Array Sorting题解

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 ai​ and ai+1​ (1≤i≤n−1), and increases both of them by 11 or decreases both of them by 11. 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.

Input

The first line contains a single integer t (1≤t≤104) — 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≤n≤3⋅105) — the number of elements in the array.

The second line of each test case contains n integers 1,2,…,a1​,a2​,…,an​ (1≤ai​≤109) — the elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 3⋅1053⋅105.

Output

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).

Sample 1

InputcopyOutputcopy
5
3
1 3 2
2
2 1
4
1 3 5 7
4
2 1 4 3
5
5 4 3 2 1
YES
NO
YES
NO
YES

Note

For the first test case, we can increase a2​ and a3​ both by 11. The array is now [1,4,3][1,4,3].

Then we can decrease a1​ and a2​ both by 11. The array is now [0,3,3][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, a1​ will always be larger than a2​. 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.

题目分析:

给一个数组,是否能够通过相邻的两个数同时加一或者同时减一得到一个不能出现递减的数列;

1.如果数字是奇数个, 根据样例第5组发现,即使该数组全部递减也可以变成我们想要的数列,所以直接输出YES即可

2.如果数字是偶数个,那么就要判断左和右,如果左边整体大于右边,那就直接寄了,输出NO,然而这种"左边整体大于右边"并非字面上的相加,而是根据右边与左边的差,如果所有的差值加和小于零,说明是左边比较大,所以输出NO,否则输出YES 

AC代码 :

#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int a[N];
int main()
{
	int n, m;
	long long sum;
	cin >> n;
	while (n--)
	{
		cin >> m;
		for (int i = 1; i <= m; i++)cin >> a[i];
		if (m % 2 == 0)
		{
			sum=0;
			for (int i = 1; i < m; i += 2)
				sum += a[i + 1] - a[i];


			(sum >=0)? printf("YES\n") : printf("NO\n");
		}
		else cout << "YES" << endl;
	}
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值