Description:
A sequence of 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 i≤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≤105 ), indicating the length of the sequence.
The second line contains n integers a1,a2,……,an ( 1≤ai≤2×109 ), indicating the integer sequence.
It’s guaranteed that the sum of n in all test cases won’t exceed 106 .
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
题目连接
判断数列是否先增后降(不能相等),直接在输入时判断标记即可。
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const double eps = 1e-5;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
int t;
int n;
ll a[maxn];
bool flag;
bool ans;
int main() {
//fropen("in.txt", "r", stdin);
scanf("%d", &t);
while (t--) {
flag = 1;
ans = 1;
scanf("%d", &n);
scanf("%lld", &a[0]);
for (int i = 1; i < n; ++i) {
scanf("%lld", &a[i]);
if (a[i] == a[i - 1]) {
ans = 0;
}
if (!ans) {
continue;
}
if (flag) {
if (a[i] < a[i - 1]) {
if (i == 1) {
ans = 0;
}
flag = 0;
}
}
else {
if (a[i] > a[i - 1]) {
ans = 0;
}
}
}
if (ans && !flag) {
printf("Yes\n");
}
else {
printf("No\n");
}
}
return 0;
}