Bear and Three Balls

Time Limit:2000MS     Memory Limit:262144KB     64bit
IO Format:
%I64d & %I64u

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

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<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,a[100],i;
int b[100];
while(~scanf("%d",&n))
{
int flag=0;
int j=0;
for(i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
for(i=0;i<n;i++)
{
if(a[i]!=a[i+1]) b[j++]=a[i];
}
for(i=0;i<j;i++)
{
if(b[i]==b[i+1]-1&&b[i]==b[i+2]-2)
{
flag+=1;
break;
}
}
if(flag!=0) printf("YES\n");
else printf("NO\n");
}
return 0;
}

或者使用unique函数进行去重

#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int n,a[100],i;
while(~scanf("%d",&n))
{
int flag=0;
for(i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
n=unique(a,a+n)-a;
for(i=0;i<n;i++)
{
if(a[i]==a[i+1]-1&&a[i+1]==a[i+2]-1) flag+=1;
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

关于去重函数的更多用法在http://blog.csdn.net/tomorrowtodie/article/details/51907471里面

【codeforces】Bear and Three Balls(排序,去重)的更多相关文章

  1. Codeforces 653A Bear and Three Balls【水题】

    题目链接: http://codeforces.com/problemset/problem/653/A 题意: 给定序列,找是否存在连续的三个数. 分析: 排序~去重~直接判断~~ 代码: #inc ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Bear and Three Balls

    链接:http://codeforces.com/problemset/problem/653/A                                                   ...

  7. 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 ...

  8. NX二次开发-C++的vector排序去重用法

    #include <algorithm> //vector排序去重 sort( BoxNum.begin(), BoxNum.end()); BoxNum.erase(unique(Box ...

  9. 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 ...

随机推荐

  1. 【Golang】解决Go test执行单个测试文件提示未定义问题

    背景 很多人记录过怎么执行Go test单个文件或者单个函数,但是要么对执行单文件用例存在函数或变量引用的场景避而不谈,要么提示调用了其它文件中的模块会报错.其实了解了go test命令的机制之后,这 ...

  2. 总是有个yumBackend.py阻止我用yum进行更新

    [Another app is currently holding the yum lock; waiting for it to exit...] 上网查了,好像是说帮我安个桌面图标的进程. 估计是 ...

  3. t-SNE 聚类

    一个有效的数据降维的方法 t-SNE,类似PCA的主成分降维分析. 参考: t-分布邻域嵌入算法(t-SNE algorithm)简单理解 t-SNE初学 很好的教程:An illustrated i ...

  4. 3-18/19 (自我练习)30多个《Ruby元编程》的spell(pattern)小例子。

    Spell,也称pattern,idiom # Around Alias:从一个重新定义的方法中调用原始的,被重命名的版本. # old_reverse是未改变的原始方法,reverse/new_re ...

  5. Android Webview 和Javascript交互,实现Android和JavaScript相互调用

    在Android的开发过程中.遇到一个新需求.那就是让Java代码和Javascript代码进行交互.在IOS中实现起来很麻烦.而在Android中相对来说容易多了.Android对这种交互进行了很好 ...

  6. jstl <fmt:formatNumber>标签

    标签用于格式化数字,百分比和货币. 如果type属性为百分比或数字,则可以使用多个数字格式属性.maxIntegerDigits和minIntegerDigits属性允许您指定数字的非分数部分的大小. ...

  7. anaconda环境变量+修改jupyter默认路径

    手贱在安装的时候没有点添加环境变量 安装好后,用anaconda prompt运行一些程序命令之类都是可以的,但是直接打开cmd就不行了,为了省事,所以决定手动添加环境变量, %\ProgramDat ...

  8. winform窗体程序运行后怎样隐藏?

    运行winform窗体,我们是怎样隐藏的呢? 例子: 1)创建简单winform窗体 2)编写隐藏窗体程序的代码 3)效果演示 1)创建一个简单的winform窗体MainForm,

  9. scrapy-redis基础和介绍

    一.scrapy-redis(0.6)依赖的环境 Scrapy >= 1.0.0                #终于过了1版本,这个太重要了,总算坑小了点,感谢那些为了解决各种scrapy与s ...

  10. 关系数据库ORMlite的用法;

    首先要在 http://ormlite.com/releases 页面下载两个jar 包: (1)core列表下的jar包: (2)android列表下的jar包. 将上面的两个jar包下载后放到An ...