zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    待解决问题集
    官方
    jsp 页面接收另一个页面传递过来的数据
    页面无法通过“${ctx}” 拿到值
    自定义 UsernamePasswordToken 报错 java.lang.ClassCastException: org.apache.shiro.authc.UsernamePasswordToken cannot be cast to
    springboot+mybatis map集合映射字段,当sql字段返回值为空时 不映射的问题
    java实现读写服务器文件
    前端模板免费下载网站
    The connection string contains invalid user information. If the username or password contains a colon (:) or an at-sign (@) then it must be urlencoded 解决方法
    springBoot -mongodb 小坑
  • 原文地址:https://www.cnblogs.com/z-712/p/7307935.html
Copyright © 2011-2022 走看看