A sequence of integers a1,a2,…,ana1,a2,…,an is called a peak, if and only if there exists exactly one integer kk such that 1<k<n1<k<n, and a1<a2a1<a2 for all ,ai−1>aiai−1>aiand for all k<i≤nk<i≤n .
Given an integer sequence, please tell us if it’s a peak or not.
Input
There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:
The first line contains an integer n(3≤n≤105)n(3≤n≤105), indicating the length of the sequence.
The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤2×1091≤ai≤2×109), indicating the integer sequence.
It’s guaranteed that the sum of in all test cases won’t exceed .
Output
For each test case output one line. If the given integer sequence is a peak, output “Yes” (without quotes), otherwise output “No” (without quotes).
Sample Input
7
5
1 5 7 3 2
5
1 2 1 2 1
4
1 2 3 4
4
4 3 2 1
3
1 2 1
3
2 1 2
5
1 2 3 1 2
Sample Output
Yes
No
No
No
Yes
No
No
就这找到一个最大值,最大值的左边递减,最大值的右边也递减,最大值不能为开头或者是结尾..看代码
# include <stdio.h>
# include <string.h>
int main(void)
{
int t, n, i, m, max, d, j, e, e1;
int a[100001];
scanf("%d", &t);
while (t --)
{
max = 0;
scanf("%d", &n);
for (i = 1; i <= n ; i++)
{
scanf("%d", &a[i]);
if (a[i] > max)
{
max = a[i];
d = i;
}
}
if (d == 1 || d == n)
printf("No\n");//开头结尾为0,结束
else
{
e = 0;
for (j = 1; j < d; j ++)
{
if (a[j] >= a[j+1])// 记得要有等号
{
e = 1;
break;
}
}//从前往最大值比较
if (e == 1)
printf("No\n");
else
{
e1 = 0;
for (j = d; j <= n-1; j ++)
{
if (a[j] <= a[j+1])// 记得等号
{
e1 = 1;
break;
}
}// 从后往前比较
if (e1 == 1)
printf("No\n");
else
printf("Yes\n");
}
}
}
return 0;
}