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.
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.
Print "YES" if the given array is unimodal. Otherwise, print "NO".
You can output each letter in any case (upper or lower).
6
1 5 5 5 4 2
YES
5
10 20 30 20 10
YES
4
1 2 1 2
NO
7
3 3 3 3 3 3 3
YES
In the first example the array is unimodal, because it is strictly increasing in the beginning (from position 1 to position 2, inclusively), that it is constant (from position 2 to position 4, inclusively) and then it is strictly decreasing (from position 4 to position 6, inclusively).
1 /* 2 题目大意:有一个序列 3 如果满足左部严格上升(可以为空),中间恒定,右部严格下降(可以为空) 4 那么就输出YES,否则输出NO 5 解题思路:按照规则走一遍,如果没走完,那么就NO,否则YES。 6 */ 7 #include <iostream> 8 using namespace std; 9 const int MAXN=105; 10 const int INF=0x3f3f3f3f; 11 int a[MAXN]; 12 int main(){ 13 int n; 14 while(cin>>n){ 15 for(int i=1;i<=n;i++) 16 cin>>a[i]; 17 a[n+1]=INF; 18 int p=2; 19 while(a[p]>a[p-1]) p++; 20 while(a [p]==a[p-1]) p++; 21 while(a[p]<a[p-1]) p++; 22 if(p<=n) cout<<"NO"<<endl; 23 else cout<<"YES"<<endl; 24 } 25 return 0; 26 }
1 #include <iostream> 2 #define N 105 3 using namespace std; 4 int n,maxn=1,a[N]; 5 bool check(){ 6 for(int i=1;i<=n;i++){ 7 if(a[i]==maxn){ 8 for(int j=i-1;j>=1;j--) 9 if(a[j]>=a[j+1]) return false; 10 while(a[i]==maxn&&i<=n) 11 i++; 12 for(int j=i;j<=n;j++) 13 if(a[j-1]<=a[j]||a[j]>=maxn) 14 return false; 15 break; 16 } 17 } 18 return true; 19 } 20 int main(){ 21 cin>>n; 22 for(int i=1;i<=n;i++) 23 cin>>a[i],maxn=max(a[i],maxn); 24 if(check()) 25 cout<<"YES"<<endl; 26 else cout<<"NO"<<endl; 27 return 0; 28 }