【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 ...
随机推荐
- C语言位域解析&符号位扩展规则
从一个例子说起: int main(void){ union{ int i; struct{ ; ; ; }bits; }num; printf("Input an integer for ...
- python导包显示No module named XXX问题
最近用sublime text写python脚本,在导包是一直显示No module named XXX. 问题描述: 首先文件夹的目录结构如下: count.py文件,代码如下: #coding=u ...
- initctl 创建自己的JOB
我们的项目需要一个启动一个外部的Jetty server.发现每次kill了这个jetty的进程后,系统会自动启动一个jetty.追查下去发现,原来是在/etc/init.d/jetty 脚本的sta ...
- English trip -- VC(情景课)9 B Outside chores 室外家务
Vocabulary focus 核心词汇 cutting the grass 修剪草坪 getting the mail 收到邮件 taking out the trash 把垃圾带出去 wal ...
- 20170731xlVba根据数据表和模板表生成新表
Public Sub SplitData() Dim Wb As Workbook Dim Sht As Worksheet Dim NewSht As Worksheet Dim arr As Va ...
- Recursive Queries CodeForces - 1117G (线段树)
题面: 刚开始想复杂了, 还以为是个笛卡尔树.... 实际上我们发现, 对于询问(l,r)每个点的贡献是$min(r,R[i])-max(l,L[i])+1$ 数据范围比较大在线树套树的话明显过不了, ...
- Please, another Queries on Array? CodeForces - 1114F (线段树,欧拉函数)
这题刚开始看成求区间$\phi$和了........先说一下区间和的做法吧...... 就是说将题目的操作2改为求$(\sum\limits_{i=l}^{r}\phi(a[i]))\%P$ 首先要知 ...
- Leetcode 77
//这似乎是排列组合的标准写法了已经class Solution { public: vector<vector<int>> combine(int n, int k) { v ...
- spark 任务运行原理
调优概述 在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以 ...
- 守护进程的创建(syslog函数)
守护进程(daemon)是指在后台运行的,没有控制终端与之相连的进程.它独立于控制终端,通常周期性的执行某种任务. 守护进程是一种很有用的进程.Linux的大多数服务器就是用守护进程的方式实现的,如I ...