codeforces - 766B【三角形判断】

本文介绍了一个关于使用三条线段构造非退化三角形的问题。通过给出的n个线段长度,文章提供了一种有效算法来判断是否能从中选出三个线段构成一个具有正面积的三角形,并附带了具体的实现代码。
B. Mahmoud and a Triangle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to use exactly 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.

Input

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.

Output

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.

Examples
Input
5
1 5 3 2 4
Output
YES
Input
3
4 1 2
Output
NO
题意:给定n个数,问能否找到三个数可以构成三角形
题解:从大到小判断相邻三个即可
代码:
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <bitset>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <cmath>
10 #include <list>
11 #include <set>
12 #include <map>
13 #define rep(i,a,b) for(int i = a;i <= b;++ i)
14 #define per(i,a,b) for(int i = a;i >= b;-- i)
15 #define mem(a,b) memset((a),(b),sizeof((a)))
16 #define FIN freopen("in.txt","r",stdin)
17 #define FOUT freopen("out.txt","w",stdout)
18 #define IO ios_base::sync_with_stdio(0),cin.tie(0)
19 #define mid ((l+r)>>1)
20 #define ls (id<<1)
21 #define rs ((id<<1)|1)
22 #define N 100005
23 #define INF 0x3f3f3f3f
24 #define INFF ((1LL<<62)-1)
25 using namespace std;
26 typedef long long LL;
27 typedef pair<int, int> PIR;
28 const double eps = 1e-8;
29 
30 int n, a[N];
31 int main()
32 {IO;
33     while(cin >> n){
34         rep(i, 1, n)    cin >> a[i];
35         sort(a+1, a+n+1);
36         int ok = 0;
37         per(i, n, 3){
38             int x = a[i], y = a[i-1], z = a[i-2];
39             if(x+y > z && x+z > y && y+z > x){
40                 ok = 1;
41                 break;
42             }
43         }
44         cout << (ok ? "YES" : "NO") << endl;
45     }
46     return 0;
47 }
View Code

 

 

转载于:https://www.cnblogs.com/Jstyle-continue/p/6379673.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值