【IndiaHacks 2016 - Online Edition (Div 1 + Div 2) ErrichtoA】【水题】Bear and Three Balls 是否有数值相差1的三个数
2016-03-21 09:16
337 查看
Bear and Three Balls
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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).
Examples
input
output
input
output
input
output
Note
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.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
int a[1010];
bool check()
{
for (int i = 2; i < 1000; ++i)
{
if (a[i - 1] && a[i] && a[i + 1])return 1;
}
return 0;
}
int main()
{
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; ++i)
{
int x;
scanf("%d", &x);
a[x] = 1;
}
puts(check() ? "YES" : "NO");
}
return 0;
}
/*
【trick&&吐槽】
>_<判断是否有三个连续数值的数,可不能用排序来做啊
【题意】
有n(50)个数t[],每个数的数值范围都为[1,1000]
让你求出是否有x,x+1和x+2这样的三个数
【类型】
水题
【分析】
计数排序是你最好的选择
【时间复杂度&&优化】
O(1000)
*/
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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).
Examples
input
4 18 55 16 17
output
YES
input
6 40 41 43 44 44 44
output
NO
input
8 5 972 3 4 1 4 970 971
output
YES
Note
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.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
int a[1010];
bool check()
{
for (int i = 2; i < 1000; ++i)
{
if (a[i - 1] && a[i] && a[i + 1])return 1;
}
return 0;
}
int main()
{
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; ++i)
{
int x;
scanf("%d", &x);
a[x] = 1;
}
puts(check() ? "YES" : "NO");
}
return 0;
}
/*
【trick&&吐槽】
>_<判断是否有三个连续数值的数,可不能用排序来做啊
【题意】
有n(50)个数t[],每个数的数值范围都为[1,1000]
让你求出是否有x,x+1和x+2这样的三个数
【类型】
水题
【分析】
计数排序是你最好的选择
【时间复杂度&&优化】
O(1000)
*/
相关文章推荐
- 【HDU 5366】The mook jong 详解
- HDU 5240 Exam (好水的题)
- 北大—1006——Biorhythms
- 时间计算(heaven.pas/cpp)
- 工作依赖(job.cpp/pas)
- 题目 英雄 (BFS)
- kmp 学习 hihocoder #1015
- HDU 1096 A+B for Input-Output Practice (VIII)
- HUST-1601 - Shepherd 暴力
- P1478
- P1035
- P1008 难度2.7
- 2020 绝对值排序
- 2021 发工资咯
- 2022 海选女主角
- 2024 C语言合法标识符
- 2025 查找最大元素
- 2026 首字母变大写
- 2027 统计元音
- 2028 Lowest Common Multiple Plus