题目链接
A sequence of n
integers a1,a2,...,an
is called a peak, if and only if there exists exactly one integer k
such that 1 < k < n
, and ai < ai+1
for all 1 <= i < k
, and ai-1 > ai
for all k < i <= 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 <= n <= 10
5
), indicating the length of the sequence.
The second line contains n
integers a1,a2,...,an
(1 <= ai <= 2 x 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;
typedef long long ll;
const int N = 1e5 + 10;
ll t,n,k1,k2,a[N];
int main(){
ios :: sync_with_stdio(false);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(cin >> t){
while(t--){
memset(a,0,sizeof a);
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = n;i >= 1;i--) a[i] -= a[i - 1];
k1 = 0;
for(int i = 2;i <= n;i++){
if(a[i] <= 0){
k1 = i;
break;
}
}
if(k1 == 0 || k1 == 2 || a[k1] == 0){//1.全部上升;2.只有下降,没有上升;3.存在相等
cout << "No" << endl;
continue;//记得continue;
}
k2 = k1;
for(int i = k1;i <= n;i++){
if(a[i] >= 0){
k2 = i;
break;
}
}
if(a[k2] == 0 || k2 != k1) cout << "No" << endl;//1.存在相等;2.在下降的时候出现上升
else cout << "Yes" << endl;
}
}
return 0;
}