Codeforces Round #396 (Div. 2) B. Mahmoud and a Triangle 贪心
2017-02-08 15:11
399 查看
B. Mahmoud and a Triangle
题目连接:
http://codeforces.com/contest/766/problem/BDescription
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.Sample Input
51 5 3 2 4
Sample Output
YESHint
题意
问你能否从n个数字中抽出来三个,使得可以构成不退化的三角形。题解:
一开始觉得好神呀……然后发现只要排个序,然后判断a[i],a[i-1],a[i+1]能否组成三角形就好了,这样贪心肯定是对的。
代码
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5+7; int n; long long a[maxn]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+1+n); int flag = 0; for(int i=2;i<=n-1;i++){ if(a[i]+a[i-1]>a[i+1]) flag = 1; } if(flag)cout<<"YES"<<endl; else cout<<"NO"<<endl; }
相关文章推荐
- Codeforces Round #396 (Div. 2) B. Mahmoud and a Triangle 贪心
- Codeforces Round #396(Div. 2)B. Mahmoud and a Triangle【上古时代题】nlogn判断三角形
- Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle
- Codeforces Round #376 (Div. 2) B. Coupons and Discounts(贪心)
- Codeforces Round #330 (Div. 2)C. Warrior and Archer(博弈,贪心)
- Codeforces Round #228 (Div. 2) C Fox and Box Accumulation(贪心)
- Codeforces Round #444 (Div. 2) D. Ratings and Reality Shows 贪心
- Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心
- Codeforces Round #280 (Div. 2) B. Vanya and Lanterns STL+贪心
- Mahmoud and a Dictionary codeforces396 div2 D
- Codeforces Round #396(Div. 2)A. Mahmoud and Longest Uncommon Subsequence【水题】
- Codeforces Round #396(Div. 2)E. Mahmoud and a xor trip
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function
- codeforces 204(Div.1 A) Little Elephant and Interval(贪心)
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor(贪心)
- Codeforces Round #410 (Div. 2) D. Mike and distribution(贪心)
- Codeforces #253 (Div. 1)C. Artem and Array ( 贪心
- Codeforces Round 374 (Div 2)D Maxim and Array 【贪心】