CodeForces 831A 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).

Example

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).

题目大意:给你一串数字问是不是unimodal(单峰的)。所谓unimodal就是数列前半部分严格单增,接下来是连续相等的数字,后面部分是严格单减的。例如 [5,?7,?11,?11,?2,?1]中[5,7,11]单增,[11,11]连续相等,[11,2,1]单减。

大致思路:开个数组,按照给出的条件简单模拟一下。先判断递增,模拟成功,计数器+1,不成功直接到下一个循环判断是否连续相等,不成功再跳到最后一个判断条件。如果是unimodal,那么会发现计数器会大于给出数组大小,详见代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 105
using namespace std;
int a[maxn];
int main()
{
    int i,n;
    while(scanf("%d",&n)==1)
    {
        memset(a,0,sizeof(a));//因为1=<a[i]<=1000,所以初始化可以直接用0
        for(i=0;i<n;i++)
            cin>>a[i];
        int p=1;
        while(a[p]>a[p-1])//顺序要对
            p++;
        while(a[p]==a[p-1])
            p++;
        while(a[p]<a[p-1])
            p++;
        if(p>n)//这里p只可能大于n或小于n
            cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}
时间: 2024-10-08 12:13:45

CodeForces 831A Unimodal Array的相关文章

codeforces 482B. Interesting Array【线段树区间更新】

题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val.就是区间l---r上的与的值为val,最后问你原来的数组是多少?如果不存在输出no 分析:分析发现要满足所有的区间,而一个点上假如有多个区间的话,这个点的值就是所有区间或的值,因为只有这样才能满足所有区间的,把所有位上的1都保存下来了,那么可以发现用线段树来维护,但是那么怎么判断满不满足条件呢?可以也用线段树,更新了之后在整个维护一遍看看满不满足题意,如

Codeforces 482B Interesting Array(线段树)

题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,现在有M个限制,每个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 否有满足的数列. 解题思路:线段树维护,每条限制等于是对l~r之间的数或上q(取且的性质,相应二进制位一定为1),那么处理完所有的 限制,在进行查询,查询对应每个l~r之间的数取且是否还等于q.所以用线段树维护取且和,修改为或操作. #include <cstdio> #include <c

CodeForces E. Lucky Array 幸运数列

CodeForces    E. Lucky Array  幸运数列 Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are n

codeforces 797 E. Array Queries【dp,暴力】

题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为止,求每次询问要转换的次数. 题解:纯暴力会TLE,所以在k为根号100000范围内dp打表 dp[i][j]表示初始p为i, k为j,需要转换几次可以大于n. 状态转移方程:dp[i][j] = dp[i+a[i]+j] + 1 #include <cstdio> #include <al

codeforces 407C Curious Array

codeforces 407C Curious Array 参考题解:https://www.cnblogs.com/ChopsticksAN/p/4908377.html 1.杨辉三角可以由多维前缀和求得. 2."搭顺风车"的思想. 3.// 右端点减去的那个数可以用组合数学的方法思考. #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define mp ma

Codeforces 1054D Changing Array

Codeforces 1054D Changing Array 做法:给定一个序列,每个数可以把在2进制k位下取反,也可以不变,在改变后,这个序列异或和不为0的区间的个数.考虑如何求出尽可能少的异或为0的序列,对序列求前缀之后,就相当与问这个前缀的序列中,有多少对的值相同,注意还有开始的0.那么对于所有数取值为min(a,~a),现在我们需要最小化,更新后同一种数中出现的相同的数对的个数,即\(C(a,2) + C(w-a,2)\),w是这种数的个数,a是取反的数的个数,当\(a = \frac

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 thi

Codeforces 86D Powerful array(莫队)

题目链接:http://codeforces.com/problemset/problem/86/D 题目: An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occu

Codeforces 722C Destroying Array(并查集)*

题目链接:http://codeforces.com/problemset/problem/722/C 题意: 有 n 个正整数序列.同时 n 个摧毁序列,从 1 到 n每次把正整数序列里对应下标的数字去掉,成为一个间隔. 问每去掉一个数字,序列中最大的连续子段和. 思路: 倒着想,这道题就变成了:从 n 到 1每次出现一个数字,若数字的两边已经存在集合,则把存在的集合合并为一个, 并更新集合的和,否则单独为一个集合.过程中始终维护序列中目前所有集合里和的最大值. 代码: #include <i