• hdu 6512 Triangle


    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(1n5×106)  which represent the number of sticks and this is followed by n positive intergers(smaller than 2311) 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
     
    很水的题,原来输入的数据是排好序的,怪不得加上排序就超时了,对于排好序的,只需要判断相邻两个的和是否大于第三个就可以了,想一想画面,很好理解。
    代码:
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    
    using namespace std;
    int n,a[5000000];
    int main() {
        while(~scanf("%d",&n)) {
            for(int i = 0;i < 2 && i < n;i ++) {
                scanf("%d",&a[i]);
            }
            int flag = 0;
            for(int i = 2;i < n;i ++) {
                scanf("%d",&a[i]);
                if(flag) continue;
                if(a[i - 2] > a[i] - a[i - 1]) {
                    flag = 1;
                }
            }
            puts(flag ? "YES" : "NO");
        }
    }
  • 相关阅读:
    HDU 5087 (线性DP+次大LIS)
    POJ 1064 (二分)
    Codeforces 176B (线性DP+字符串)
    POJ 3352 (边双连通分量)
    Codeforces 55D (数位DP+离散化+数论)
    POJ 2117 (割点+连通分量)
    POJ 1523 (割点+连通分量)
    POJ 3661 (线性DP)
    POJ 2955 (区间DP)
    LightOJ 1422 (区间DP)
  • 原文地址:https://www.cnblogs.com/8023spz/p/10800067.html
Copyright © 2020-2023  润新知