文章标题Gym 100971C :triangles

14 篇文章 0 订阅

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值