A - Peak (第十五届浙江省程序设计竞赛)

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


题目坑点很多,特别是比赛的时候很容易wr

#include 

using namespace std;
typedef long long ll;
ll th[100010];
int main()
{
    int num;
    scanf("%d", &num);
    while(num --){
        int n;
        scanf("%lld", &n);
        for(int i = 0; i < n; i ++) scanf("%lld", &th[i]);
        int flag = 0;
        int flag1 = 0;
        int i;
        for(i = 1; i < n;){
            if(th[i] >  th[i - 1]){
                flag ++;
                while(th[i] > th[i - 1] && i < n){
                    i ++;
                }
            }else if(th[i] < th[i - 1]){
                if(flag == 0) break;
                flag1 ++;
                while(th[i] < th[i - 1] && i < n) i ++;
            }else break;
        }
        if(flag == 1 && flag1 == 1 && i == n){
            printf("Yes\n");
        }else printf("No\n");
    }
    return 0;
}

你可能感兴趣的:(思维)