You are given an array a[0…n−1] of n integers. This array is called a “valley” if there exists exactly one subarray a[l…r] such that:
0≤l≤r≤n−1,
al=al+1=al+2=⋯=ar,
l=0 or al−1>al,
r=n−1 or ar<ar+1.
Here are three examples:
The first image shows the array [3,2,2,1,2,2,3], it is a valley because only subarray with indices l=r=3 satisfies the condition.
The second image shows the array [1,1,1,2,3,3,4,5,6,6,6], it is a valley because only subarray with indices l=0,r=2 satisfies the codition.
The third image shows the array [1,2,3,4,3,2,1], it is not a valley because two subarrays l=r=0 and l=r=6 that satisfy the condition.
You are asked whether the given array is a valley or not.
Note that we consider the array to be indexed from 0.
Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the array.
The second line of each test case contains n integers ai (1≤ai≤109) — the elements of the array.
It is guaranteed that the sum of n over all test cases is smaller than 2⋅105.
Output
For each test case, output “YES” (without quotes) if the array is a valley, and “NO” (without quotes) otherwise.
You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).
Example
inputCopy
6
7
3 2 2 1 2 2 3
11
1 1 1 2 3 3 4 5 6 6 6
7
1 2 3 4 3 2 1
7
9 7 4 6 9 9 10
1
1000000000
8
9 4 4 5 9 4 9 10
outputCopy
YES
YES
NO
YES
YES
NO
Note
The first three test cases are explained in the statement.
分析
- 题意:看看一个序列是否存在,当前某段序列(也可以是一个数),是否小于紧挨着它的左边那个数,是否小于相邻的右边那个数;也就是让这个序列或者这个单独的数,小于两边的数;此题要求一个序列只能存在一种情况,也就是有多个l,r都能满足条件,就不符合题意;
- l,r可以指向一段连续相同的数的序列,也可以共同指向一个单独的数,所以我们可以把重复元素去掉,直接比较单个数;一开始想着全部去重,这是不对的,因为同一个数字可能不是连续出现的,全去重了就不对了;然后考虑到局部部分去重,把连续的相同的数去掉,然后存放在一个数组里;
- 然后对第一个数、最后一个数特判,其他的数分别和两边的数相比较记录有几种满足的情况即可;
#include<bits/stdc++.h>
using namespace std;
int t, n, cnt;
int a[200005];
int b[200005];//把连续的数删掉的得到的数组
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cnt = 0;//当前数组的元素个数
b[cnt++] = a[0];
//把连续的数去重
for (int i = 1; i < n; ++i) {
if (a[i] != a[i - 1])
b[cnt++] = a[i];
}
int sum = 0;//满足山谷的情况数
if (cnt == 1 || cnt == 2) {
cout << "YES" << endl;
} else {
//特判第一个数
if (b[0] < b[1])
sum++;
//中间的数和前后比
for (int i = 1; i < cnt - 1; ++i) {
if (b[i] < b[i - 1] && b[i] < b[i + 1])
sum++;
}
//特判最后一个数
if (b[cnt - 1] < b[cnt - 2])
sum++;
if (sum > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
return 0;
}