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. MariaDB 服务器在 MySQL Workbench 备份数据的时候出错如何解决

    服务器是运行在 MariaDB 10.2 上面的,在使用 MySQL Workbench 出现错误: mysqldump: Couldn't execute 'SELECT COLUMN_NAME, ...

  2. P3377 【模板】左偏树(可并堆)

    //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack- ...

  3. Spring Data Rest如何暴露ID字段

    package com.example.demo.config; import com.example.demo.model.Comp; import com.example.demo.model.P ...

  4. EchoService

    dubbo为consumer端的代理对象实现了EchoService接口. 使用示例: <dubbo:reference id="hello" interface=" ...

  5. MessageFormat格式化数字

    使用MessageFormat格式化数字,有一个很隐蔽的技巧点: public static void main(String[] args) { MessageFormat mf = new Mes ...

  6. linux tcpdump 抓包

    tcpdump是linux命令行下常用的的一个抓包工具,记录一下平时常用的方式,测试机器系统是ubuntu 12.04. tcpdump的命令格式 tcpdump的参数众多,通过man tcpdump ...

  7. jstl <fmt:formatNumber>标签

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

  8. JavaScript学习总结(三)——逻辑And运算符详解

    在JavaScript中,逻辑 AND 运算符用双和号(&&)表示 1 var bTrue = true; 2 var bFalse = false; 3 var bResult = ...

  9. jsp jsp常用指令

    jsp指令是为jsp引擎设计的,他们并不直接产生任何可见输出,而只是告诉引擎如何处理jsp页面中的其余部分. jsp中的指令 page指令 include指令 taglib指令 jsp指令的基本语法 ...

  10. js 实现智能输入数字

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...