Codeforces 766b-mahmoud and a Triangle

Source: Internet
Author: User
Tags printf time limit

Mahmoud and a Triangle time limit per test 2 seconds memory limit per test megabytes input standard input output stand ARD output

Mahmoud have n line segments, the i-th of them have length AI. Ehab challenged him to use exactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn ' t accept challenges unless he's sure he can win, so he asked you to tell him if he should accept the Challe Nge. 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 the line segments or change any length. A non-degenerate triangle is a triangle with positive area. Input

The first line contains a single integer n (3≤n≤105)-the number of line segments Mahmoud have.

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

In the-line print ' YES ' if he can choose exactly three line segments and form a non-degenerate triangle with them, an D "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.

Test instructions: Give the number of N, ask if you can choose 3 numbers to form a triangle

Problem-solving ideas: First, all the numbers from small to large to sort, if there are three of the number of triangles can be formed, then the adjacent three number must be possible


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;
#define LL Long Long

int main ()
{
    int n,a[100090];
    while (~SCANF ("%d", &n))
    {for
        (int i=1;i<=n;i++)
            scanf ("%d", &a[i]);
        Sort (a+1,a+1+n);
        int flag=0;
        for (int i=3;i<=n;i++)
            if (a[i]<a[i-1]+a[i-2]) flag=1;
        if (flag) printf ("yes\n");
        else printf ("no\n");
    }
    return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.