暑期集训之Bear and Three Balls

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 45 and 3, or balls with sizes 9091 and 92. But he can't choose balls with sizes 55 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 3031 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).

Example
Input
4
18 55 16 17
Output
YES
Input
6
40 41 43 44 44 44
Output
NO
Input
8
5 972 3 4 1 4 970 971
Output
YES

这道题虽然说是sort排序专题的题目,但却是也是一道小思维题,说了一大堆,说白了就是看选的球里面有没有三个连续大小的球,既然我们知道这个了,其实就很简单了,不过,虽然看上去挺简单的,但依旧用到了一点小技巧,详解在代码里面,代码如下:

#include<stdio.h>
#include<string.h>
int main()
{ 
    int n,flag=0;
    int k[1005];//这个是放球的尺寸的数组 
    int m[1005];//这个是标记数组 
    memset(m,0,sizeof(m)); //归零操作现在很常见,所以要多加熟练,这里归零的话是为了方便下面的标记操作 
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
    	scanf("%d",&k[i]);
    	m[k[i]]=1;//这里就是标记操作了,把拥有的尺寸的球都给标记出来 
	}
	for(int i=1;i<=1000;i++)//这个循环其实是一个查找的过程,从尺寸1开始到1000,看有没有三个连续尺寸的球被我们标记出来了 
	{
		if(m[i]==1&&m[i+1]==1&&m[i+2]==1)
		flag=1;//flag标记也很常用了,尤其在循环里面用的很多 
	}
	if(flag==1)
	printf("Yes\n");
	else
	printf("No\n");
	return 0;
}//想通了这道题也还是非常简单的ORZ 

今天的专题训练最后还有一个简单贪心的题,但无奈都不知道贪心是啥QAQ,看了看别人的博客也一时半会没搞懂,但会努力学习的ORZ,今天就到此结束~\(≧▽≦)/~啦啦啦


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值