IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题
題目連接:
http://www.codeforces.com/contest/653/problem/A
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 sizes 5, 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 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).
Sample Input
4
18 55 16 17
Sample Output
YES
Hint
題意
給你n個(gè)數(shù),問你能不能從這些數(shù)里面抽出三個(gè)數(shù)
使得這三個(gè)數(shù)都不相同,且這三個(gè)數(shù)能夠滿足a[2]=a[1]+1,a[3]=a[2]+1
題解:
數(shù)據(jù)范圍很小,怎么做都可以
可以直接排個(gè)序,去重,然后check就好了。
代碼
#include<bits/stdc++.h> using namespace std;int a[103],tot=0; map<int,int> H; int main() {int n;scanf("%d",&n);for(int i=1;i<=n;i++){int x;scanf("%d",&x);if(H[x])continue;H[x]++;a[tot++]=x;}if(tot<3)return puts("NO"),0;sort(a,a+tot);for(int i=0;i+2<tot;i++){if(a[i]==a[i+1]-1&&a[i]==a[i+2]-2)return puts("YES"),0;}return puts("NO"),0;}轉(zhuǎn)載于:https://www.cnblogs.com/qscqesze/p/5296463.html
總結(jié)
以上是生活随笔為你收集整理的IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谈谈对线程与进程的理解
- 下一篇: HorizontalScrollView