The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple -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
原题网站:
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5752
题意:给你一个数组让你求这个数组是不是一个山峰数组,就这个数组有先单调递增之后再单调递减两种情况
分别用flag1,flag2来标记是否有单调递增和单调递减情况,并且判断在递减情况是否有递增情况,但是要注意如果有相同的情况是不满足递增和递减要直接输出No(这里wa一次)
代码:
#include<bits/stdc++.h> using namespace std; int a[5000000]; int main() { std::ios::sync_with_stdio(false); int t; cin>>t; while(t--){ int n; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } int flag=0; int flag1=0,flag2=0,flag4=0; int flag3=0; for(int i=1;i<n;i++){ if(a[i]==a[i-1]){//判断是否有相同的值的情况 flag4=1; } if(!flag){//标记是否有单调递增 if(a[i]>a[i-1]){ flag1=1; } if(a[i]<a[i-1]){//标记是否单调递减 flag=1; flag2=1; } } else{//标记是否在单调递减的时候还有单调递增的情况 if(a[i]>a[i-1]){ flag3=1; break; } } } if(flag3==1||(flag1+flag2!=2)||flag4==1)cout<<"No"<<endl; else cout<<"Yes"<<endl; } return 0; }