Round #424 A. Unimodal Array

Posted 浅忆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

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
Note

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的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #424 (Div. 2) A-C

Codeforces Round #424 Div. 1

Codeforce Round #424

Codeforces Round #424 (Div. 2) D 思维 E set应用,树状数组

Codeforces VK Cup Finals #424 Div.1 A. Office Keys(DP)

Round #424 B. Keyboard Layouts