IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题

A. Bear and Three Balls

题目连接:

http://www.codeforces.com/contest/653/problem/A

Description

Limak is a little polar bear. He has n balls, the i-th ball has size ti.

Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy:

No two friends can get balls of the same size.
No two friends can get balls of sizes that differ by more than 2.
For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2).

Your task is to check whether Limak can choose three balls that satisfy conditions above.

Input

The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has.

The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball.

Output

Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes).

Sample Input

4
18 55 16 17

Sample Output

YES

Hint

题意

给你n个数,问你能不能从这些数里面抽出三个数

使得这三个数都不相同,且这三个数能够满足a[2]=a[1]+1,a[3]=a[2]+1

题解:

数据范围很小,怎么做都可以

可以直接排个序,去重,然后check就好了。

代码

#include<bits/stdc++.h>
using namespace std;

int a[103],tot=0;
map<int,int> H;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        if(H[x])continue;
        H[x]++;
        a[tot++]=x;
    }
    if(tot<3)return puts("NO"),0;
    sort(a,a+tot);
    for(int i=0;i+2<tot;i++)
    {
        if(a[i]==a[i+1]-1&&a[i]==a[i+2]-2)
            return puts("YES"),0;
    }
    return puts("NO"),0;

}

你可能感兴趣的:(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题)