• 【codeforces 766B】Mahmoud and a Triangle


    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    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.

    Examples
    input
    5
    1 5 3 2 4
    output
    YES
    input
    3
    4 1 2
    output
    NO
    Note
    For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.

    【题目链接】:http://codeforces.com/contest/766/problem/B

    【题意】

    给你n条边;
    问你能不能从中挑出3条边来组成一个三角形;(只要是三角形就行)

    【题解】

    可以这样;

    c<=b<=a
    先把边的长度升序排;
    如果是三角形那么;
    a+b>c
    a+c>b即c>b-a
    b+c>a即c>a-b
    这里b-a是个负数,所以不用理他
    只考虑a-b就好;
    而想让a-b最小,肯定是选长度相邻的两条边;
    这样从大到小,维护i..n这个区间范围内a-b的最小值mi;
    看看a[i]是不是大于mi;一旦有符合的就ok了;
    (至于a+b>c,因为a和b都在i..n这个范围内选,所以a+b>c肯定成立的)

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int MAXN = 1e5+100;
    
    int a[MAXN],n;
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);
        rep1(i,1,n)
            rei(a[i]);
        sort(a+1,a+1+n);
        int d = a[n]-a[n-1];
        rep2(i,n-2,1)
            {
                if (d<a[i])
                {
                    puts("YES");
                    return 0;
                }
                d = min(d,a[i+1]-a[i]);
            }
        puts("NO");
        return 0;
    }
  • 相关阅读:
    JMeter参数化及断言的使用
    JMeter基础概念
    postman关联
    postman参数化
    postman发送请求的简单操作
    postman的简单介绍及运用
    javascript小记五则:用JS写一个图片左右自由滚动的“跑马灯”效果
    javascript小记四则:用JS写一个滚动横条文字,可以根据需要进行修改;
    javascript小记三则:ASP.NET启动web调试,窗体自动放大的方法
    JavaScript小记二则:接上一节:用.net写Textbox控件关于数字的判断的另一则方法
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626633.html
Copyright © 2020-2023  润新知