• 百度之星初赛A轮 A 度度熊拼三角 贪心


    度度熊拼三角

     
     Accepts: 2536
     
     Submissions: 4433
     Time Limit: 2000/1000 MS (Java/Others)
     
     Memory Limit: 65536/65536 K (Java/Others)
    Problem Description

    度度熊有 NN 根木棒,每根木棒的长度为a_iai​​。

    现在要挑选其中的三根,问能拼出的三角形的最大周长是多少。

    如果不能拼成任何一个三角形,输出 -11。

    Input

    多组数据(不超过1010组),读到EOF结束。

    对于每一组数据:

    第一行一个数 NN 表示木棒数量。

    第二行一共 NN 个数,描述每一根木棒的长度。

    1 leq N leq 10001N1000

    木棒长度都是不超过100000100000的正整数

    Output

    对于每一组数据,输出一个数表示答案。

    Sample Input
    3
    1 1 100
    7
    1 9 9 90 2 2 4
    Sample Output
    -1
    22

     分析:要使周长最大,肯定是组成的三边尽量大,排序所有边,从大到小枚举,第一个满足条件的三边即所求

    AC代码:

    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <bitset>
    #include <cstring>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #define ls (r<<1)
    #define rs (r<<1|1)
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    typedef long long ll;
    const ll maxn = 1e6+10;
    const ll mod = 998244353;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    ll n, a[1010];
    bool cmp( ll p, ll q ) {
        return p > q;
    }
    bool ok( ll x, ll y, ll z ) {
        if( x+y <= z || x+z <= y || y+z <= x ) {
            return false;
        }
        return true;
    }
    int main() {
        ios::sync_with_stdio(0);
        while( cin >> n ) {
            memset(a,0,sizeof(a));
            for( ll i = 0; i < n; i ++ ) {
                cin >> a[i];
            }
            if( n < 3 ) {
                cout << -1 << endl;
                continue;
            }
            sort(a,a+n,cmp);
            ll num = -1;
            for( ll i = 2; i < n; i ++ ) {
                if( ok(a[i],a[i-1],a[i-2]) ) {
                    num = a[i]+a[i-1]+a[i-2];
                    break;
                }
            }
            cout << num << endl;
        }
        return 0;
    }
    

      

    彼时当年少,莫负好时光。
  • 相关阅读:
    js 常用正则表达式
    深度学习
    开通自动订阅功能前:输入银行业务信息
    VUE学习九,生命周期
    Setting Windows server firewall by powershell
    Install iis web server using powershell
    转 FAL[server, ARC3]: FAL archive failed Error 16401 creating standby archive log file at host
    springboot admin 监控
    二进制手指计数法
    SpringBoot集成Swagger-Bootstrap-UI
  • 原文地址:https://www.cnblogs.com/l609929321/p/9504128.html
Copyright © 2020-2023  润新知