IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)——A - Bear and Three Balls(unique函数的使用)
2 seconds
256 megabytes
standard input
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.
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.
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).
4
18 55 16 17
YES
6
40 41 43 44 44 44
NO
8
5 972 3 4 1 4 970 971
YES
In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes18, 16 and 17.
In the second sample, there is no way to give gifts to three friends without breaking the rules.
In the third sample, there is even more than one way to choose balls:
- Choose balls with sizes 3, 4 and 5.
- Choose balls with sizes 972, 970, 971.
题目本身不难,就是问你有没有三个连续的数字,比如1 2 3,45 46 47这样的,如果你用等差数列去判断,那存在一个坑点就是1 2 2 3,这样不是等差数列,但是也是可行的即输出YES。因此用了unique函数,这个函数的作用是把一个排序后的数组合并连续区间内相同的数字,比如1 2 2 2 3会变成1 2 3,那么剩下的两个2去哪了呢,被移到了数组的末尾。此函数本身返回一个完成合并区间的超尾指针,跟.end()差不多。因此使用之后的数组大小应该是N=unique(list,list+n)-list;
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
int list[100];
int main (void)
{
int n,i,j,k;
while (cin>>n)
{
memset(list,0,sizeof(list));
bool flag=0;
for (i=0; i<n; i++)
{
cin>>list[i];
}
sort(list,list+n);
int *p=unique(list,list+n);
int k=p-list;
for (i=0; i<k-2; i++)
{
if(list[i]+1==list[i+1]&&list[i+1]+1==list[i+2]&&list[i]+2==list[i+2])
{
flag=true;
break;
}
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)——A - Bear and Three Balls(unique函数的使用)的更多相关文章
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing
B. Bear and Compressing 题目链接 Problem - B - Codeforces Limak is a little polar bear. Polar bears h ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树
E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tre ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) D. Delivery Bears 二分+网络流
D. Delivery Bears 题目连接: http://www.codeforces.com/contest/653/problem/D Description Niwel is a littl ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) C. Bear and Up-Down 暴力
C. Bear and Up-Down 题目连接: http://www.codeforces.com/contest/653/problem/C Description The life goes ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing 暴力
B. Bear and Compressing 题目连接: http://www.codeforces.com/contest/653/problem/B Description Limak is a ...
- 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 ...
- 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 ...
- IndiaHacks 2016 - Online Edition (CF) . D
这题思路很简单,二分m,求最大流是否大于等于x. 但是比赛过程中大部分的代码都被hack了... 精度问题,和流量可能超int 关于精度问题,这题真是提醒的到位,如果是先用二分将精度控制在10^-8左 ...
随机推荐
- Predicate和Consumer接口的使用
// Predicate 判断是否拥有资格,Consumer 改变输入的值 案例 public static MyTest2 getV(MyTest2 a, Predicate<MyTe ...
- ECshop安装提示cls_image::gd_version() 和不支持JPEG
ecshop版本:ECShop_V2.7.3_UTF8_release1106php 版本 5.5--------------------------------------------------- ...
- python基础一 day15 面试题
# def demo():# for i in range(4):# yield i## g=demo()## g1=(i for i in g)# g2=(i for i in g1)## prin ...
- JavaScript 获取对象中第一个属性
使用 Object.keys(object) 可以取出属性名为数组,但会打乱顺序 严格意义上对象中是只有映射关系而没有顺序的,但是在存储结构里是有顺序的,如果想获取存储结构里的第一个属性可以使用for ...
- DROP LANGUAGE - 删除一个过程语言
SYNOPSIS DROP [ PROCEDURAL ] LANGUAGE name [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP LANGUAGE 将删除曾注 ...
- 01_7_cookies
01_7_cookies 1. cookies 1.1服务器可以向客户端写内容 1.2只能是文本内容 1.3客户端可以阻止服务器写入 1.4只能拿到自己webapp写入的东西 1.5Cookie分为两 ...
- NSRegularExpression 使用
需求: // 后台返回的某个实体 reminder = { cost = , type = , template = 可免费做某事{time}分钟,超过将按{cost}K元收费, time = } t ...
- VueJS坎坷之路111---_self.$scopedSlots.default is not a function
VueJs + Element 话不多说,直接贴错: _self.$scopedSlots.default is not a function <el-table stripe border r ...
- JavaScript之基操
局部变量前面要加var 如 var name = "jiahuai" 全局变量 name = "jiahuai" 写完每一行JavaScript代码用;号隔 ...
- 【转】浅谈对主成分分析(PCA)算法的理解
以前对PCA算法有过一段时间的研究,但没整理成文章,最近项目又打算用到PCA算法,故趁热打铁整理下PCA算法的知识.本文观点旨在抛砖引玉,不是权威,更不能尽信,只是本人的一点体会. 主成分分析(PCA ...