Round #424 A. Unimodal Array
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.
InputThe 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.
OutputPrint "YES" if the given array is unimodal. Otherwise, print "NO".
You can output each letter in any case (upper or lower).
Examples input6output
1 5 5 5 4 2
YESinput
5output
10 20 30 20 10
YESinput
4output
1 2 1 2
NOinput
7output
3 3 3 3 3 3 3
YESNote
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 }
Round #424 A. Unimodal Array