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
代码:
#include<iostream>
using namespace std;
int a[100000];
int main()
{
int t;
cin >> t;
while (t--)
{
int n,flag=-1;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 0; i < n; i++)
{
if (i != 0 && i != n - 1)//判断峰值是否处于中间位置
{
if (a[i-1]<a[i] && a[i]>a[i+1])//找到峰值
{
flag = i;//记录下标
}
}
}
for (int i = 0; i < n; i++)
{
if (i>flag)//判断峰值后面的数
{
if (a[i] >= a[i-1])//判断是否比峰值大
{
flag = 0;
}
}
if (i < flag)//判断峰值前面的数
{
if (a[i] <=a[i-1])//判断是否比峰值大
{
flag = 0;
}
}
}
flag ? puts("Yes") : puts("No");//flag=0,输出No,反之,输出Yes;
}
}