Bear and Three Balls
链接:http://codeforces.com/problemset/problem/653/A
2 seconds
256 megabytes
standard input
standard output
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 and6 (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.
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.
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).
4
18 55 16 17
YES
6
40 41 43 44 44 44
NO
8
5 972 3 4 1 4 970 971
YES
In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes18, 16 and 17.
In the second sample, there is no way to give gifts to three friends without breaking the rules.
In the third sample, there is even more than one way to choose balls:
- Choose balls with sizes 3, 4 and 5.
- Choose balls with sizes 972, 970, 971
AC代码:
不是原创代码,看的百度博客上的思路,自己当时不会做。。。。现在想想其实不难,,,
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int x,int y)
{ return x<y;
}
int main()
{
int n,i,j,k,d,s,e,q;
int a[];
while(scanf("%d",&n)!=EOF)
{
k=;
for(i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
for(i=;i<n-;i++)
{
for(j=i+;j<n;j++)
{
s=a[j]-a[i];
if(s==||s==)
{
for(q=j+;q<n;q++)
{
d=a[q]-a[j];
e=a[q]-a[i];
if((e==)&&(d==))
k++;
} }
}
}
if(k>) printf("YES\n");
else printf("NO\n");
}
return ;
}
Bear and Three Balls的更多相关文章
- codeforces 653A Bear and Three Balls
A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【codeforces】Bear and Three Balls(排序,去重)
Bear and Three Balls Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 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 ...
- codeforces 653A A. Bear and Three Balls(水题)
题目链接: A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)——A - Bear and Three Balls(unique函数的使用)
A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 653 A. Bear and Three Balls——(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2))
传送门 A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input ...
- H - 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 bal ...
- Codeforces 653A Bear and Three Balls【水题】
题目链接: http://codeforces.com/problemset/problem/653/A 题意: 给定序列,找是否存在连续的三个数. 分析: 排序~去重~直接判断~~ 代码: #inc ...
- C. Bear and Colors
C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- PHP容器--Pimple运行流程浅析
需要具备的知识点 闭包 闭包和匿名函数在PHP5.3.0中引入的. 闭包是指:创建时封装周围状态的函数.即使闭包所处的环境不存在了,闭包中封装的状态依然存在. 理论上,闭包和匿名函数是不同的概念.但是 ...
- Android 任何位置的可移动悬浮窗
刚好要做这块的东西,所以网上翻了下资料,百度出来的基本上都是:默认起始位置左上角,还不能改动,一改动起始位置,第二次拖动就不正常了~~ 下面直接附上任意位置可拖动的源码(由于是demo写的比较乱): ...
- Vijos 1001 谁拿了最多奖学金
题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1 ...
- 如何将md文件转换成带目录的html文件
配置环境node 去官网下一个node安装包,下一步下一步: 由于现在的node都自带npm,直接 npm install i5ting_toc 这样安装好了i5ting_toc这个包, 进入你实现准 ...
- 多线程下System.Security.Cryptography.Aes CreateDecryptor报“Safe handle has been closed”的解决方案
因为系统需要对一些核心数据进行预加载以保证查询速度. 所以在application_start 事件中启用了后台线程对相关的数据进行加载并解密(为了保证解密的效率,将AES对像做了静态对像来保存:pr ...
- JavaWeb 后端 <十三> 之 监听器 JSTL国际化
1. 监听器 1.1 概述 监听器: 主要是用来监听特定对象的创建或销毁.属性的变化的! 是一个实现特定接口的普通java类! 对象: 自己创建自己用 (不用监听) 别人创建自己用 (需要监听) ...
- 如何在java中用Arraylist中实现冒泡排序的问题
众所周知,冒泡排序法在一般数组中就3步, if(a<b){ temp=a; a=b; b=temp; } 然而,在集合中就不是简单的交换一下了,因为交换之后,必须保证新的值被重新设置到集合中去. ...
- Java语言的9个主要特性
Java作为时下很流行的一门编程语言,受到很多人的热爱,那么它有哪些特性呢?一起来看看吧. 1.Java语言是简单的 Java语言的语法与C语言和C++语言很接近,使得大多数程序员很容易学习和使用.另 ...
- 使用C#在VS中开发:未处理AccessViolationException “System.AccessViolationException”类型的未经处理的异常
未处理AccessViolationException: “System.AccessViolationException”类型的未经处理的异常在System.Data.dll中发生 其他信息:尝试读 ...
- Iterator、for..of,for...in和自定义遍历器**
Iterator.for..of,for...in和自定义遍历器 Iterator: var arr = [1,2,3,4,5]; var ite = arr[Symbol.iterator](); ...