Codeforces Round #396 (Div. 2) B. Mahmoud and a Triangle 贪心
B. Mahmoud and a Triangle
题目连接:
http://codeforces.com/contest/766/problem/B
Description
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
5
1 5 3 2 4
Sample Output
YES
Hint
题意
问你能否从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) D. Mahmoud and a Dictionary 并查集
D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...
- Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle
地址:http://codeforces.com/contest/766/problem/A A题: A. Mahmoud and Longest Uncommon Subsequence time ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message dp
C. Mahmoud and a Message 题目连接: http://codeforces.com/contest/766/problem/C Description Mahmoud wrote ...
- Codeforces Round #396 (Div. 2) A. Mahmoud and Longest Uncommon Subsequence 水题
A. Mahmoud and Longest Uncommon Subsequence 题目连接: http://codeforces.com/contest/766/problem/A Descri ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
地址:http://codeforces.com/contest/766/problem/E 题目: E. Mahmoud and a xor trip time limit per test 2 s ...
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
地址:http://codeforces.com/contest/766/problem/C 题目: C. Mahmoud and a Message time limit per test 2 se ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip 树形压位DP
题目链接:http://codeforces.com/contest/766/problem/E Examples input 3 1 2 3 1 2 2 3 out 10 题意: 给你一棵n个点 ...
随机推荐
- Win10新增功能快捷键大全
原文地址:http://wenwen.sogou.com/z/q703976788.htm贴靠窗口:Win + 左/右 > Win + 上/下 > 窗口可以变为 1/4 大小放置在屏幕 4 ...
- POJ - 1753 Flip Game(状压枚举)
https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...
- bzoj千题计划264:bzoj3884: 上帝与集合的正确用法
http://www.lydsy.com/JudgeOnline/problem.php?id=3884 欧拉降幂公式 #include<cmath> #include<cstdio ...
- Here’s just a fraction of what you can do with linear algebra
Here’s just a fraction of what you can do with linear algebra The next time someone wonders what the ...
- nmap - 网络扫描
NMap,Network Mapper 最早是Linux下的网络扫描和嗅探工具包 网络链接扫描; nmap -PT 192.168.1.1-111 # 先ping在扫描主机开放端口 nmap -O 1 ...
- vue实践中的狗血事件之:mock数据引发的血坑
在项目实践中,遇到了这么一档子事 开发环境下,很快乐,什么事儿都没有,于是想打包一下测一下自动登录的效果 好家伙,一开始登录没有效,改来改去,最后连路由都切换不了, 明明开发环境下好好的,为毛打包后就 ...
- 升级openssh到最新版本
首先,下载最新版本,传到服务器:http://mirror.aarnet.edu.au/pub/OpenBSD/OpenSSH/portable/ 安装 cd /root/ mkdir ssh_upg ...
- FTP主动/被动原理
FTP 主动模式 1.客户端用大于1024的高位端口发起初始化连接到vsftp服务器的21端口 2.vsftp服务器的21端口主动与客户端大于1024的高位端口建立控制连接 3.vsftp服务器的20 ...
- Linux 串口、usb转串口驱动分析(2-2) 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4186852 Linux 串口.usb转 ...
- python psutil监控系统资源【转】
通过 运用 Python 第三方 系统 基础 模块, 可以 轻松 获取 服务 关键 运营 指标 数据,包括 Linux 基本 性能. 块 设备. 网卡 接口. 系统 信息. 网络 地址 库 等 信息. ...