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左 ...
随机推荐
- UVA 11572 Unique snowflakes (滑窗)
用set,保存当前区间出现过的数字,如果下一个数字没有出现过,加入,否则删掉左端点,直到没有重复为止 #include<bits/stdc++.h> using namespace std ...
- js判断是否为app
var ua = navigator.userAgent; var isapp = ua.match("lenovomallapp") == null ? 0 : 1;
- PAT (Basic Level) Practise (中文)- 1018. 锤子剪刀布 (20)
http://www.patest.cn/contests/pat-b-practise/1018 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统 ...
- HTML5<footer>元素
HTML5中<footer>元素是用来描述文档中的底部信息,比如:版本,版权,作者,链接声明,联系信息,时间等等. 实例: <footer> <p>这是一个底部的信 ...
- NOIP2016 toy
题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...
- 洛谷 P5016 龙虎斗
输入兵营总数.兵营人数.以m分界. 然后输入s1个兵到了p1兵营. 最终我们要求的是把s2个兵放到哪个兵营使龙虎双方气势差距最小. 第一要把每个兵营的气势算出来,并且加到它所属的阵营里(<m是龙 ...
- 【Ubuntu】ubuntu基本操作命令
本文主要是用于记录ubuntu中会使用到的命令,但是有不是特别常用的,用于自己后续查阅使用. 1.查询ubuntu版本信息 方法一: cat /etc/issue 方法二: sudo lsb_rele ...
- 预防cdn链接失效,无缝切换本地文件
如今的前端项目追求的不仅仅是能用能看的程度,而是愈发追求项目的性能,对用户体验的影响.而现在的开发工具在性能优化方面也替我们做很大一部分的工作,想必大家对CDN的使用都是轻车熟路了,但是大家有没有考虑 ...
- 代理工具--fiddle
正则匹配 1)前缀为“EXACT:”表示完全匹配:只有match=rules时,才匹配 2)无前缀表示基本搜索,表示搜索到字符串就匹配:只要match中包含了rules的字符串,即可 3)前缀为“NO ...
- IAR生成bin,HEX文件
1.生成bin,hex文件 options->output converter->output format binary:.bin文件:intel extended:hex文件. 生成的 ...