zoukankan      html  css  js  c++  java
  • IndiaHacks 2016

    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个数,问你能不能从这些数里面抽出三个数

    使得这三个数都不相同,且这三个数能够满足a[2]=a[1]+1,a[3]=a[2]+1

    题解:

    数据范围很小,怎么做都可以

    可以直接排个序,去重,然后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;
    
    }
  • 相关阅读:
    angularjs学习笔记—工具方法
    js日期格式转换的相关问题探讨
    vue路由原理剖析
    如何减少UI设计师产品与前端工程师的沟通成本
    前端优化带来的思考,浅谈前端工程化
    前端入门方法
    自写juqery插件实现左右循环滚动效果图
    前端大综合
    前端收集
    如何在代码中减少if else语句的使用
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5296463.html
Copyright © 2011-2022 走看看