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
standard input
output
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, 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).

Examples
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
题目大意:
就是给定 n 个数,让你求的就是是否存在三个连续的数。。。

解题思路:

这个题本来这么简单不想写的,但是突然发现一个函数还是比较不错的,unique(),就是这个函数,本来我是打算暴力的后来一寻思太麻烦了,就用集合写的,写到一半突然发现迭代器 it 不能加2,所以炸了,后来就用一个数组存的每一个*it 的数,有点小麻烦,具体看我第一个代码
My First AC Code:
<span style="font-size:18px;">#include <iostream>
#include <algorithm>
#include <set>
#include <cstdio>
#include <cstring>
using namespace std;
set <int> s;
set <int> ::iterator it;
int a[105];
int main()
{
    int n, x;
    while(cin>>n)
    {
        for(int i=0; i<n; i++)
        {
            cin>>x;
            s.insert(x);
        }
        memset(a, 0, sizeof(a));
        int cnt = 0;
        for(it=s.begin(); it!=s.end(); it++)
            a[cnt++] = *it;///每个都存一遍 实在是没招了 set学的不好呀。。。
        bool ok = 0;
        for(int i=1; i<cnt; i++)
        {
            if(a[i]==a[i-1]+1 && a[i]+1==a[i+1])
            {
                ok = 1;
                break;
            }
        }
        if(ok)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}</span>

接下来我要说的就是这篇博客的重点了(有没有想到高中老师呀...)介绍一个 STL 算法,unique(),这个函数是用来去重的,这还是我一队友告诉我的呢,我感觉自己好low啊,我详细的说一下:
unique的功能是去除相邻的重复元素(只保留一个),其实它并不是真正意思上的把重复的元素删除,只是把重复的元素移到后面去了,然后还是保存到了原数组中,然后 返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
int tmp = unique(a,a+n)-a;我们只需要写上这么一句话,就行了,别忘了最后还得减a因为这个函数返回的就是地址,所以要减去,而tmp这个变量里存的就是去重之后的数组的长度,还是比较不错的吧,上我第二个代码:
My Second AC Code:

<span style="font-size:18px;">#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int a[105];
int main()
{
    int n, x;
    while(cin>>n)
    {
        for(int i=0; i<n; i++)
            cin>>a[i];
        sort(a,a+n);
        int tmp = unique(a,a+n)-a;///头文件 <algorithm>,还是挺好的,去重用的
        bool ok = 0;
        for(int i=1; i<tmp; i++)
        {
            if(a[i]==a[i-1]+1 && a[i]+1==a[i+1])
            {
                ok = 1;
                break;
            }
        }
        if(ok)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}
</span>


目录
相关文章
vbs鼠标方法——模拟鼠标按键
'*********************************************************************** ' 代码开始 '*********************************************************************** Set mouse=New SetMouse mouse.
5341 0
WK
|
算法 决策智能
粒子群算法的缺点是什么
粒子群算法(PSO)虽具优点,但存在明显缺点:易陷局部最优、收敛精度低、难解离散及组合优化问题、缺乏精密搜索方法、理论基础薄弱、参数选择困难、收敛速度受问题复杂度影响。为克服这些问题,研究者提出引入动态惯性权重、调整学习因子、混合算法等改进策略,提高算法性能与适用范围,但仍需进一步研究以应对更复杂多样的问题。
WK
1520 1
|
SQL 关系型数据库 MySQL
【YashanDB知识库】MySQL的FIND_IN_SET如何在YashanDB改写
本文来自YashanDB官网,探讨了MySQL中使用`FIND_IN_SET`函数在YashanDB中不兼容的问题及解决方法。由于YashanDB的`tinyint`类型无法参与条件运算,而MySQL的Boolean类型是`tinyint`的同义词,导致直接迁移时出现错误。文章通过分析原因,提出改写方案:将`where find_in_set(`替换为`where 0 &lt; find_in_set(`,从而实现兼容性调整,方便用户快速迁移和改写SQL语句。
|
Web App开发 前端开发 JavaScript
和 Houdini, CSS Paint API 打个招呼吧
Hodini 的出现将赋予开发者前所未有的控制页面视觉表现的能力。这个项目的第一步是实现 CSS Paint API。本篇将解释为什么 Houdini 的到来让人如此兴奋,以及向读者展示如何开始使用 Paint API。
1951 0
|
8天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
9504 25
|
8天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
2739 14
|
3天前
|
人工智能 安全 应用服务中间件
NGINX 爆出两处高危漏洞 可导致未经身份验证的远程代码执行
NGINX曝出两个高危漏洞(CVE-2026-123633和CVE-2026-123007),CVSS 9.2分,均属HTTP/2内存安全缺陷,无需认证即可远程执行代码。影响自2015年起的多个版本,静态站点亦受威胁。修复版本为开源版1.30.7/1.31.9。(239字)
1311 1
NGINX 爆出两处高危漏洞 可导致未经身份验证的远程代码执行
|
6天前
|
传感器 安全 区块链
Windows 电脑运行安卓应用教程:开源工具 ApkStart 双击 APK 即装、独立窗口运行
ApkStart 是一款开源Windows工具,让安卓APP像原生软件一样运行:双击APK即安装、生成真实图标、独立窗口启动、开始菜单可搜索。无需传统模拟器,内置QEMU运行时,支持数据互通、ARM兼容与命令行操作。(239字)
|
11天前
|
人工智能 前端开发 API
阿里云Qwen4-Max 大模型详解:定位、特征、能力、适用场景与相对Qwen3-Max差异解析
2026年11月11日,阿里发布第四代旗舰模型Qwen4-Max预览版,首发千问AI平台Token Plan并接入千问办公、Qoder等入口。该版本聚焦知识理解、世界感知、编程与Agentic四项升级,主打复杂编程、专业办公、前端设计与专业软件操作等真实任务。预览期为11月11日10:00至11月25日24:00,仅支持Token Plan调用,到期后模型标识下线;限时2折、新用户再享5折,最低1折可体验。