codeforces - 766B【三角形判斷】

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.ios

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.less

Input

The first line contains single integer n (3 ≤ n ≤ 105) — the number of line segments Mahmoud has.ide

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the lengths of line segments Mahmoud has.spa

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.3d

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
相關文章
相關標籤/搜索