码迷,mamicode.com
首页 > 其他好文 > 详细

Peak

时间:2018-04-29 14:26:00      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:class   input   sequence   --   ++   cin   multiple   multi   clu   

A sequence of \(n\) integers \(a_1, a_2, \dots, a_n\) is called a peak, if and only if there exists exactly one integer \(k\) such that \(1 < k < n\), and \(a_i < a_{i+1}\) for all \(1 \le i < k\), and \(a_{i-1} > a_i\) for all \(k < i \le 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 \(T\), indicating the number of test cases. For each test case:

The first line contains an integer \(n\) (\(3 \le n \le 10^5\)), indicating the length of the sequence.

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \times 10^9\)), indicating the integer sequence.

It‘s guaranteed that the sum of \(n\) in all test cases won‘t exceed \(10^6\).

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<bits/stdc++.h>

using namespace std;
int a[1000005];
int main()
{
    int t;;
    cin>>t;
    while(t--){
        int f=1;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int pos=-1;
        for(int i=0;i<n;i++){
            if(i!=0&&i!=n-1){
                if(a[i-1]<a[i] && a[i]>a[i+1]){
                    pos=i;
                }
            }
        }
        //cout<<pos<<" "<<a[pos]<<endl;
        for(int i=0;i<n;i++){
            if(i<pos){
                if(a[i]>=a[i+1]) f=0;
            }
            if(i>pos){
                if(a[i]>=a[i-1]) f=0;
            }
        }
        f?puts("Yes"):puts("No");
    }
}

Peak

标签:class   input   sequence   --   ++   cin   multiple   multi   clu   

原文地址:https://www.cnblogs.com/Roni-i/p/8970910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!