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 ...
随机推荐
- 用mybatis实现dao的编写或者实现mapper代理
一.mybatis和hibernate的区别和应用场景hibernate:是一个标准的ORM框架(对象关系映射).入门门槛较高的,不需要写sql,sql语句自动生成了.对sql语句进行优化.修改比较困 ...
- 【LeetCode】332. Reconstruct Itinerary
题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...
- ovs+dpdk numa感知特性验证
0.介绍 本测试是为了验证这篇文章中提到的DPDK的NUMA感知特性. 简单来说,在ovs+dpdk+qemu的环境中,一个虚拟机牵涉到的内存共有三部分: DPDK为vHost User设备分配的De ...
- angularLoad(用以异步加载js文件)
angularLoad(用以异步加载js文件) 使用方法: 1.执行命令 下载 lib npm install angular-load --save 2.index.html引用js <scr ...
- ASP.NET 导出EXCEL文件处理多对应排列的
这次项目遇到了一个导出excel需要对应排列的问题.本来在做这个项目之前都基本没做过excel导出的菜鸡,这次强行做还是有些忐忑的,加上那个表的结构比较奇特. 废话不多说,先介绍表结构吧 是数据 ...
- Angular基础(一)
AngularJS有五个主要核心特性,如下介绍: 双向数据绑定 -- 实现了把model与view完全绑定在一起,model变化,view也变化,反之亦然. 模板 -- 在AngularJS中,模板相 ...
- 百度云bcc建站
一.购买百度云服务 1.百度云bcc购买网页http://bce.baidu.com/product/bcc.html 2.买完后管理:http://console.bce.baidu.com/bcc ...
- Merge使用
Role r = new Role(); r.setName("TEST"); r.setDescription("123"); r.setLevel(2); ...
- 设计模式 - 观察者模式(JDK)
定义:观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新. 对象: 抽象主题角色:每个抽象主题角色都可以有任意数量的观察者.抽象主题提供可 ...
- POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)
POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...