【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 ...
随机推荐
- Idea设置默认不折叠一行的函数
- Unity中的文件
Unity中的文件大致分为一下几类: 1.资源文件: 导入后,除非是修改,否则不会变化的文件.例如:fbx文件.贴图文件.音频文件.视频文件.动画文件等. 这些文件在导入到Unity的时候,都会进行转 ...
- 37mysql 表操作
创建表 #语法: create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] ); #注意: 1. 在同一张 ...
- CentOS7 install apache
1. yum install httpd 2. config /etc/httpd/conf/httpd.conf <VirtualHost *:80> ServerName www.l ...
- 利用nodeJs anywhere搭建本地服务器环境
1.npm install anywhere -g 如果是mac系统会提示你权限不够,需要在代码前加上 sudo获取管理员权限.即sudo npm install anywhere -g. 2.安装完 ...
- POJ-3414 Pots (BFS)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- 阿里云ECS服务器自定义端口无法访问问题记录
记住阿里云ECS服务器有个安全组!!! 购买了阿里云服务器的时候,购买界面那里是可以勾选默认的几个端口是否开启的,服务器默认勾了22端口,使用户能登录服务器. 当我们在服务器里面配置nginx,开启自 ...
- 应用多种变形CSS3
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- http1.0 1.1 2.0区别
http1.0 1.1 2.0区别 转载:https://blog.csdn.net/linsongbin1/article/details/54980801/ 1.HTTP1.0 1.1区别 (1) ...
- [Vue warn]: Invalid prop: custom validator check failed for prop "xxx".问题
在用vue+ui框架(iview.elementui等)做项目,会遇到这种问题 这样的,点那都报错,千辛万苦的付出,却找不到问题在哪 其实很简单,报错都显示出那个组件的问题了 ‘<Form&g ...