【codeforces】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
4
18 55 16 17
YES
6
40 41 43 44 44 44
NO
8
5 972 3 4 1 4 970 971
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(排序,去重)的更多相关文章
- Codeforces 653A Bear and Three Balls【水题】
题目链接: http://codeforces.com/problemset/problem/653/A 题意: 给定序列,找是否存在连续的三个数. 分析: 排序~去重~直接判断~~ 代码: #inc ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Bear and Three Balls
链接:http://codeforces.com/problemset/problem/653/A ...
- 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 ...
- NX二次开发-C++的vector排序去重用法
#include <algorithm> //vector排序去重 sort( BoxNum.begin(), BoxNum.end()); BoxNum.erase(unique(Box ...
- 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 ...
随机推荐
- boosting方法
概述 Boosting基本思想: 通过改变训练数据的概率分布(训练数据的权值分布),学习多个弱分类器,并将它们线性组合,构成强分类器. Boosting算法要求基学习器能对特定的数据分布进行学习,这可 ...
- 使用NativeExtension向AIR app 添加Activity和BroadCastReceiver(2)
开发: Android项目 新建一个针对NativeExtension的Android项目,实现相应的FREContext,FREExtension和FREFunction等方法,同时新建一个Acti ...
- Codeforces 448E - Divisors
448E - Divisors 思路: dfs.注意如果是1,直接返回,因为1的因子还是1. 因为x因子的因子还是x的因子,所以可以事先处理好x因子的因子在x因子中的位置. 不用这个方法也可以,用ma ...
- NGUI实现简单的倒计时组件
using System; using UnityEngine; public enum ETimerType { CommonFormat, // 78 77 76 75 ... TimeForma ...
- Python3 基本语法学习
1.查看Python版本及打印“Hellow World!”: 需要注意的是:在打印“Hello World”之前一定要先执行 python,否则会报无法 “无法初始化设备 PRN”,如图: 2.查看 ...
- UEditor自动调节宽度
UEditor自动调节宽度 一.总结 一句话总结:ueditor是网页的产物,没有API我们照样可以像调网页元素那样调,一样的,这里是改变插件的css样式实现 启示: 百度 文档 引擎 ueditor ...
- t-SNE 聚类
一个有效的数据降维的方法 t-SNE,类似PCA的主成分降维分析. 参考: t-分布邻域嵌入算法(t-SNE algorithm)简单理解 t-SNE初学 很好的教程:An illustrated i ...
- php--------获取当前时间、时间戳
首先需要知道的是在php中获取时间方法是date(),在php中获取时间戳方法有time().strtotime().下面分别说明. date() 格式为:date($format, $timesta ...
- LICEcap 和 FS Capture入门教程
上一篇介绍了如何使用 Visio 图形图表工具,文中贴了一张gif图,留言的小伙伴们迫不及待想知道如何录制 GIF 图,强哥姑且卖弄一次,把 PC 端截图工具和教程分享给大家,分别为 LICEcap ...
- Windows系统下修改Erlang默认路径
新建.erlang文件: io:format("consulting .erlang in ~p~n",[element(2, file:get_cwd())]). c:cd(&q ...