A sequence of integers is called a peak, if and only if there exists exactly one integer such that , and for all , and for all .
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 , indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the length of the sequence.
The second line contains integers (), 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
思路;模拟做即可。先找出一个峰,在对峰的两边进行判断,最后与总数比较。
注意点;数组开的100000即可,不要开大容易TLM。
注意题目给的条件,特判即可。
#include<stdio.h>
int a[100100];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,n,flag,cnt=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
if(a[1]>a[2]||a[n]>a[n-1])
{printf("No\n");
continue;
}
for(i=1;i<n;i++)
{if(a[i]>a[i+1])//前面的大于后面的,那么说明到峰了
{flag=i;
break;
}
}//标记这个峰
for(i=1;i<flag;i++)
{
if(a[i]<a[i+1])
cnt++;
}
for(i=flag;i<n;i++)
{
if(a[i]>a[i+1])
cnt++;
}
if(cnt==n-1)
printf("Yes\n");
else printf("No\n");
}
return 0;
}