文章标题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;}
- 文章标题Gym 100971C :triangles
- Gym 100971C Triangles
- GYM 100971 C.Triangles(水~)
- 文章标题 Gym 100971K : Palindromization
- 文章标题c语言快排;
- CF 229C Triangles
- Codeforces 229C Triangles
- codeforces 229C Triangles
- Codeforces 229C Triangles
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- JAVA基础--JAVA的四种引用
- Hibernate各种主键生成方式配置
- RNN和LSTM
- mysql登录 出现Access denied for user 'root'@'localhost' (using password:YES) 解决方案
- 为了往外看看的,一点特技.
- 文章标题Gym 100971C :triangles
- MAC中关于手动卸载软件和插件
- CSS3实现动画-飞翔的小鸟
- Java多线程面试题
- Struts2传参乱码问题
- python函数的参数传递方式
- Java 集合框架个人总结
- HTML5 canvas标签-3 简单灰度化的实现
- Python 与尾递归优化