文章标题Gym 100971C :triangles

来源:互联网 发布:国家旅游局大数据 编辑:程序博客网 时间:2024/05/01 09:43

triangles

Description

standard input/output
Announcement

Statements
There is a set of n segments with the lengths li. Find a segment with an integer length so that it could form a non-degenerate triangle with any two segments from the set, or tell that such segment doesn’t exist.

Input

The first line contains a single integer n(2 ≤ n ≤ 200000) — the number of segments in the set.

The second line contains n integers li separated by spaces (1 ≤ li ≤ 109) — the lengths of the segments in the set.

Output

If the required segment exists, in the first line output «YES» (without quotes). In this case in the second line output a single integer x — the length of the needed segment. If there are many such segments, output any of them.

If the required segment doesn’t exist, output «NO» (without quotes).

Sample Input

Input
2
3 4
Output
YES
2
Input
3
3 4 8
Output
YES
6
Input
3
3 4 9
Output
NO
题意:有n条边的长度,要求是否存在另外的一条边,使得与n条边中的任意两条边形成三角形,存在的话就输出YES并输出其中的一种可能,不存在就输出NO。
分析:由三角形存在定理两边之和大于第三边,两边之差小于第三边可知,我们需要求出这n条边中两边只差最大值max(|a-b|),和两边之和的最小值min(c+d);所求的x需满足max(|a-b|)< x

#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<map>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;int n;long long a[200005];int main (){    while (scanf ("%d",&n)!=EOF){        for (int i=0;i<n;i++){            scanf ("%lld",&a[i]);        }        sort (a,a+n);//排序        int left=a[n-1]-a[0];//找两边只差最大值        int right=a[0]+a[1];//找两边值和最小值        if (right-left>=2){            printf ("YES\n");            printf ("%d\n",left+1);        }        else printf ("NO\n");    }    return 0;}
0 0
原创粉丝点击