zoukankan      html  css  js  c++  java
  • Codeforces Round #396 (Div. 2) B. Mahmoud and a Triangle 贪心

    B. Mahmoud and a Triangle

    题目连接:

    http://codeforces.com/contest/766/problem/B

    Description

    Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to use exactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn't accept challenges unless he is sure he can win, so he asked you to tell him if he should accept the challenge. Given the lengths of the line segments, check if he can choose exactly 3 of them to form a non-degenerate triangle.

    Mahmoud should use exactly 3 line segments, he can't concatenate two line segments or change any length. A non-degenerate triangle is a triangle with positive area.

    Input

    The first line contains single integer n (3 ≤ n ≤ 105) — the number of line segments Mahmoud has.

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the lengths of line segments Mahmoud has.

    Output

    In the only line print "YES" if he can choose exactly three line segments and form a non-degenerate triangle with them, and "NO" otherwise.

    Sample Input

    5
    1 5 3 2 4

    Sample Output

    YES

    Hint

    题意

    问你能否从n个数字中抽出来三个,使得可以构成不退化的三角形。

    题解:

    一开始觉得好神呀……

    然后发现只要排个序,然后判断a[i],a[i-1],a[i+1]能否组成三角形就好了,这样贪心肯定是对的。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+7;
    int n;
    long long a[maxn];
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            cin>>a[i];
        sort(a+1,a+1+n);
        int flag = 0;
        for(int i=2;i<=n-1;i++){
            if(a[i]+a[i-1]>a[i+1])
                flag = 1;
        }
        if(flag)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
  • 相关阅读:
    钾 动态规划
    镁 细节
    锌 填坑计划
    钠 GZY整理贪心
    javascript+Java 实现MD5加密登录密码
    FCKeditor2.6.4图片上传,中文名乱码,红叉各种问题解决
    Java 随机生成验证码,支持大小写字母、数字、随机字体
    BI BI系统监控
    使用ETL控件还是存储过程
    fsync与数据库日志刷新
  • 原文地址:https://www.cnblogs.com/qscqesze/p/6378303.html
Copyright © 2011-2022 走看看