国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

【codeforces 766B】Mahmoud and a Triangle

2019-11-11 00:50:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard 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 Note For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.

【題目鏈接】:http://codeforces.com/contest/766/problem/B

【題意】 給你n條邊; 問你能不能從中挑出3條邊來(lái)組成一個(gè)三角形;(只要是三角形就行)

【題解】 可以這樣; 設(shè) c<=b<=a 先把邊的長(zhǎng)度升序排; 如果是三角形那么; a+b>c a+c>b即c>b-a b+c>a即c>a-b 這里b-a是個(gè)負(fù)數(shù),所以不用理他 只考慮a-b就好; 而想讓a-b最小,肯定是選長(zhǎng)度相鄰的兩條邊; 這樣從大到小,維護(hù)i..n這個(gè)區(qū)間范圍內(nèi)a-b的最小值mi; 看看a[i]是不是大于mi;一旦有符合的就ok了; (至于a+b>c,因?yàn)閍和b都在i..n這個(gè)范圍內(nèi)選,所以a+b>c肯定成立的) 【完整代碼】

#include <bits/stdc++.h>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)#define mp make_pair#define pb push_back#define fi first#define se second#define rei(x) scanf("%d",&x)#define rel(x) scanf("%I64d",&x)typedef pair<int,int> pii;typedef pair<LL,LL> pll;const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};const double pi = acos(-1.0);const int MAXN = 1e5+100;int a[MAXN],n;int main(){ //freopen("F://rush.txt","r",stdin); rei(n); rep1(i,1,n) rei(a[i]); sort(a+1,a+1+n); int d = a[n]-a[n-1]; rep2(i,n-2,1) { if (d<a[i]) { puts("YES"); return 0; } d = min(d,a[i+1]-a[i]); } puts("NO"); return 0;}
上一篇:順序串基本操作

下一篇:poj1046

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 襄汾县| 扶余县| 永安市| 南丹县| 阳泉市| 苍溪县| 会泽县| 景东| 晋城| 北票市| 嘉兴市| 肥西县| 太仆寺旗| 旺苍县| 瓦房店市| 乐东| 台中县| 曲周县| 苍南县| 房山区| 阳西县| 德州市| 包头市| 桐柏县| 交城县| 介休市| 思茅市| 万宁市| 新营市| 通辽市| 玛沁县| 建瓯市| 盱眙县| 阿拉善盟| 桐庐县| 辉南县| 池州市| 余庆县| 兴义市| 天峻县| 银川市|