hdu6512 Triangle

Triangle

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1206    Accepted Submission(s): 288


 

Problem Description

After Xiaoteng took a math class, he learned a lot of different shapes, but Xiaoteng's favorite triangle is because he likes stable shapes, just like his style. 

After the Xiaoxun knew it, he wanted to give a triangle as a gift to Xiaoteng. He originally planned to do one, but he was too tired. So he decided to bring some wooden sticks to Xiaoteng and let Xiaoteng make a triangle himself. 

One day, Xiaoxun brought n sticks to Xiaoteng. Xiaoteng wanted to try to use three of them to form a triangle, but the number is too much, Xiaoteng stunned, so he can only ask for your help.

 

 

Input

There are mutiple test cases.

Each case starts with a line containing a integer  n(1≤n≤5×106)  which represent the number of sticks and this is followed by n positive intergers(smaller than 231−1) separated by spaces.

 

 

Output

YES or NO for each case mean Xiaoteng can or can't use three of sticks to form a triangle.

 

 

Sample Input

 

4 1 2 3 4

 

 

Sample Output

 

YES

 

 

Source

2019中山大学程序设计竞赛(重现赛)

解题思路

对于一组数,如果都不能构成三角形,最小的就是斐波那契数列,因为n不超过(2^31) -1,而斐波那契数列到第50项差不多就超过了这个范围,所以如果超过55了就不用处理了,肯定是可以的,小于的排序比较即可。

代码如下

#include <iostream>
#include <cstdio>
#include <algorithm>
#define maxn 5000005
using namespace std;
int a[maxn];
int main()
{
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 1; i <= n; i ++){
            scanf("%d", &a[i]);
        }
        if(n > 55){
            printf("YES\n");
            continue;
        }
        else if(n < 3){
            printf("NO\n");
            continue;
        }
        sort(a + 1, a + n + 1);
        bool flg = false;
        for(int i = 3; i <= n; i ++){
            if(a[i - 1] + a[i - 2] > a[i]){
                flg = true;
                break;
            }
        }
        if(flg)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值