codeforces 653A . Bear and Three Balls
2016-07-19 10:44
330 查看
Bear and Three Balls(sort)
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status
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, 5and 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
Input
Output
Input
Output
Input
Output
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status
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, 5and 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
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
排序+去重
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int n; int a[55]; int b[55]; while(scanf("%d",&n)!=EOF) { int falg=0,i,k=0; for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); b[0]=a[0]; for(i=1;i<n;i++) { if(a[i]!=a[i-1]) b[++k]=a[i]; } for(i=0;i<=k-2;i++) { if((b[i+1]-b[i]==1)&&(b[i+2]-b[i]==2)) { falg=1; break; } } if(falg) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- mysql求不同字段的最值
- HDU 5701 中位数计数
- 看别人的C/C++代码时发现自己所不知道的语法~
- Linux ext2/ext3文件系统
- css兼容小问题
- 51单片机串口实验时波特率怎么调才合适
- java版http请求(防止乱码)
- Struts2 自定义拦截器(方法拦截器)
- Android 判断某个点是否在扇形或者不规则图形区域内
- hdu 5289 RMQ运用
- Latent Semantic Analysis(LSA/ LSI)算法介绍(转)
- main(int argc,char* argv[ ])
- Play1.2.x源代码阅读之 classloading
- 使用Cocoapods创建私有podspec
- struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
- oracle 恢复删除的表数据
- 线程间通信方式(wait和notify的使用)
- html中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
- Oracle中使用REGEXP_SUBSTR,regexp_replace,wm_concat函数
- 谁说linux下无游戏,十大免费linux大型游戏集合