Mahmoud and a Triangle (CodeForces
来源:互联网 发布:淘宝闺蜜投诉入口 编辑:程序博客网 时间:2024/05/22 06:58
Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to useexactly 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.
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.
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.
51 5 3 2 4
YES
34 1 2
NO
For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.
分析:首先进行排序。因为三角形满足任意两边之和大于第三边(a+b>c),任意两边之差小于第三边(c-b<a)。所以使得a+b尽可能大,c尽可能小。那么可以取连续的三个数进行判断即可。
#include <iostream>#include <algorithm>using namespace std;long num[100005];int main(){ int n; cin>>n; for(int i = 0; i<n; i++) cin>>num[i]; sort(num, num+n); for(int i = 0; i + 2<n; i++){ if(num[i] + num[i+1] > num[i+2]){ cout<<"YES"<<endl; return 0; } } cout<<"NO"<<endl; return 0;}
- Mahmoud and a Triangle (CodeForces
- 【codeforces 766B】Mahmoud and a Triangle
- Codeforces Mahmoud and a Triangle (水
- codeforces 766B Mahmoud and a Triangle
- CodeForces - 766B Mahmoud and a Triangle
- codeforces 766 B Mahmoud and a Triangle
- Codeforces 766B-Mahmoud and a Triangle
- CodeForces 766B Mahmoud and a Triangle
- codeforces 766B Mahmoud and a Triangle
- Mahmoud and a Triangle 【codeforces】 【水题】
- Mahmoud and a Triangle
- B. Mahmoud and a Triangle
- Codeforces766B Mahmoud and a Triangle
- Mahmoud and a Dictionary CodeForces
- Mahmoud and a Dictionary CodeForces
- Mahmoud and a Dictionary CodeForces
- Mahmoud and a Message CodeForces
- Mahmoud and a Dictionary CodeForces
- Spark算子[08]:combineByKey详解
- 2017-12-11
- JAVA打印回型数
- 【Scikit-Learn 中文文档】内核近似
- Vanilla RNN是什么,它背后又隐藏了什么?
- Mahmoud and a Triangle (CodeForces
- PostgreSQL for Data Architects.pdf 英文原版 免费下载
- scala语言基础学习
- docker教程(3)--volume
- 【Scikit-Learn 中文文档】成对的矩阵, 类别和核函数
- poj1907 nim博弈
- nginx配置说明
- leeetcode-40. Combination Sum II
- 简单的vim配置