codeforces362B

Petya and Staircases

 CodeForces - 362B 

题意:

一个小男孩要上楼梯,他一次可以走1个台阶或2个台阶或3个台阶,但是有一些台阶是脏的,他不想走在脏台阶上。一共有n个台阶和m个脏台阶,他最开始在第1个台阶上,要走到第n个台阶。问小男孩能不能不踩到脏台阶的前提下走到n个台阶。

Input

The first line contains two integers n and m (1 ≤ n ≤ 1090 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).

Output

Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".

Examples

Input
10 5
2 4 8 3 6
Output
NO
Input
10 5
2 4 5 7 9
Output
YES

sol:因为有一步步爬的存在,所以只要不是连续三个及以上的脏的台阶,都可以过去
Ps:特判首尾是脏的情况
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=3005;
int n,m,a[N];
int main()
{
    int i;
    R(n); R(m);
    for(i=1;i<=m;i++) R(a[i]);
    sort(a+1,a+m+1);
    if(a[1]==1||a[m]==n) return 0*puts("NO");
    for(i=3;i<=m;i++) if(a[i-2]+1==a[i-1]&&a[i-1]+1==a[i])
    {
        return 0*puts("NO");
    }
    puts("YES");
    return 0;
}
/*
input
10 5
2 4 8 3 6
output
NO

input
10 5
2 4 5 7 9
output
YES
*/
View Code

 

 
posted @ 2019-04-02 21:10  yccdu  阅读(217)  评论(0编辑  收藏  举报