【codeforces】Bear and Three Balls(sort)
来源:互联网 发布:子弹图纸尺寸图片编程 编辑:程序博客网 时间:2024/04/30 13:27
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 sizes5, 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 30and 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
418 55 16 17
YES
640 41 43 44 44 44
NO
85 972 3 4 1 4 970 971
YES
题解:实际上是找是否存在三个连续的数字
12345678910111213141516171819202122232425262728293031323334#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int n,a[55],b[55]; while(~scanf("%d",&n)) { int ans=1,f=0,k=0; for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); b[0]=a[0]; for(int i=1;i<n;i++) { if(a[i]!=a[i-1]) //消去重复的数字 b[++k]=a[i]; } for(int i=2;i<=k;i++) { if((b[i]-b[i-1]==1)&&(b[i]-b[i-2]==2)) { f=1; break; } } if(f) printf("YES\n"); else printf("NO\n"); } return 0; }
- 【codeforces】Bear and Three Balls(sort)
- Bear and Three Balls(sort)
- B - Bear and Three Balls(sort)
- Bear and Three Balls (sort)
- 【codeforces】Bear and Three Balls
- Bear and Three Balls CodeForces
- CodeForces 653A Bear and Three Balls(sort+去重)
- Codeforces 653A: Bear and Three Balls(技巧,水)
- 【CodeForces】653A - Bear and Three Balls(计数)
- Codeforces 653A Bear and Three Balls
- codeforces->653A. Bear and Three Balls
- 【CodeForces】[653A]Bear and Three Balls
- CodeForces 653 A. Bear and Three Balls
- CodeForces 653A Bear and Three Balls
- 【Codeforces】-653A-Bear and Three Balls
- codeforces 653A . Bear and Three Balls
- codeforces 653A. Bear and Three Balls
- 【codeforces 653 A Bear and Three Balls】
- 单调栈(poj -- 2559)
- 网络修复-可以上qq但是网页等打不开
- KMP算法 HDU 1686 Oulipo 也就是乌力波啦~~
- 星际之门(一)(cayley定理n阶完全图的生成数个数,快速幂)
- HOJ 1003题 题解
- 【codeforces】Bear and Three Balls(sort)
- Git入门(7)-撤销修改
- POJ 2553The Bottom of a Graph
- android 子线程中更新界面?被ProgressBar给迷惑了
- Nova 是如何统计 OpenStack 计算资源
- 平衡搜索树之AVLTree
- Ekka Dokka
- 四大组件之特殊Service(IntentService)的使用
- Busybox 登陆后如何设置是否需要用户名和密码进入文件系统