A. Unimodal Array
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Array of integers is unimodal, if:
it is strictly increasing in the beginning;
after that it is constant;
after that it is strictly decreasing.
The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent.
For example, the following three arrays are unimodal: [5, 7, 11, 11, 2, 1], [4, 4, 2], [7], but the following three are not unimodal: [5, 5, 6, 6, 1], [1, 2, 1, 2], [4, 5, 5, 6].
Write a program that checks if an array is unimodal.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array.
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 1 000) — the elements of the array.
Output
Print “YES” if the given array is unimodal. Otherwise, print “NO”.
You can output each letter in any case (upper or lower).
Examples
input
6
1 5 5 5 4 2
output
YES
input
5
10 20 30 20 10
output
YES
input
4
1 2 1 2
output
NO
input
7
3 3 3 3 3 3 3
output
YES
就是模拟,分清楚各种情况
代码
#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline int read(){
int f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return f*x;
}
/*------------------------------*/
const int MAXN = 1e5;
const int MAXM = 1E8;
int arr[MAXN];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&arr[i]);
if(n==1) puts("YES");
else{
int i;
if(arr[2]-arr[1]==0){
for(i=2;i<=n;i++)
if(arr[i]!=arr[i-1]) break;
if(i==n+1) puts("YES");
else {
if(arr[i]-arr[i-1]>0) puts("NO");
else{
for(i;i<=n;i++)
if(arr[i]-arr[i-1]>=0) break;
if(i==n+1) puts("YES");
else puts("NO");
}
}
}else if(arr[2]-arr[1]>0) {
for(i=2;i<=n;i++)
if(arr[i]-arr[i-1]<=0) break;
if(i==n+1) puts("Yes");
else {
if(arr[i]-arr[i-1]==0){
for(i;i<=n;i++)
if(arr[i]!=arr[i-1]) break;
if(i==n+1) puts("YES");
else {
if(arr[i]-arr[i-1]>0) puts("NO");
else {
for(i;i<=n;i++)
if(arr[i]-arr[i-1]>=0) break;
if(i==n+1) puts("YES");
else puts("NO");
}
}
}else{
for(i;i<=n;i++)
if(arr[i]-arr[i-1]>=0) break;
if(i==n+1) puts("YES");
else puts("NO");
}
}
} else {
for(i=2;i<=n;i++){
if(arr[i]-arr[i-1]>=0) break;
}
if(i==n+1) puts("YES");
else puts("NO");
}
}
return 0;
}