zoukankan      html  css  js  c++  java
  • codeforces

    B. Mahmoud and a Triangle
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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
    题意:给定n个数,问能否找到三个数可以构成三角形
    题解:从大到小判断相邻三个即可
    代码:
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <bitset>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <cmath>
    10 #include <list>
    11 #include <set>
    12 #include <map>
    13 #define rep(i,a,b) for(int i = a;i <= b;++ i)
    14 #define per(i,a,b) for(int i = a;i >= b;-- i)
    15 #define mem(a,b) memset((a),(b),sizeof((a)))
    16 #define FIN freopen("in.txt","r",stdin)
    17 #define FOUT freopen("out.txt","w",stdout)
    18 #define IO ios_base::sync_with_stdio(0),cin.tie(0)
    19 #define mid ((l+r)>>1)
    20 #define ls (id<<1)
    21 #define rs ((id<<1)|1)
    22 #define N 100005
    23 #define INF 0x3f3f3f3f
    24 #define INFF ((1LL<<62)-1)
    25 using namespace std;
    26 typedef long long LL;
    27 typedef pair<int, int> PIR;
    28 const double eps = 1e-8;
    29 
    30 int n, a[N];
    31 int main()
    32 {IO;
    33     while(cin >> n){
    34         rep(i, 1, n)    cin >> a[i];
    35         sort(a+1, a+n+1);
    36         int ok = 0;
    37         per(i, n, 3){
    38             int x = a[i], y = a[i-1], z = a[i-2];
    39             if(x+y > z && x+z > y && y+z > x){
    40                 ok = 1;
    41                 break;
    42             }
    43         }
    44         cout << (ok ? "YES" : "NO") << endl;
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    分布式任务调度 xxl-job
    【线上】 select * from xxoo where 1=1应用挂掉
    【死磕ES】七、基础检索
    【死磕ES】四、基本操作
    【死磕ES】三、基本概念
    【死磕ES】二、搭建环境
    Mac共享文件夹
    微信小程序下拉刷新,上拉加载
    微信小程序textarea输入框出现[object Object]
    微信小程序official-account的使用
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6379673.html
Copyright © 2011-2022 走看看