• spoj


     题意:给定n个数,分别表示边长,找到任意两边之和大于第三边的可能的个数。

    You have N (3 ≤ N ≤ 2,000) wooden sticks, which are labeled from 1 to N. The i-th stick has a length of Li (1 ≤ Li ≤ 1,000,000). Your friend has challenged you to a simple game: you will pick three sticks at random, and if your friend can form a triangle with them (degenerate triangles included), he wins; otherwise, you win. You are not sure if your friend is trying to trick you, so you would like to determine your chances of winning by computing the number of ways you could choose three sticks (regardless of order) such that it is impossible to form a triangle with them.

    Input

    The input file consists of multiple test cases. Each test case starts with the single integer N, followed by a line with the integers L1, ..., LN. The input is terminated with N = 0, which should not be processed.

    Output

    For each test case, output a single line containing the number of triples.

    Example

    Input:
    3
    4 2 10
    3
    1 2 3
    4
    5 2 9 6
    0
    
    Output:
    1
    0
    2

    For the first test case, 4 + 2 < 10, so you will win with the one available triple. For the second case, 1 + 2 is equal to 3; since degenerate triangles are allowed, the answer is 0.

    #include"bits/stdc++.h"
    #define MAXN 300005
    #define INF 1000000000
    #define MOD 1e9+7
    #define F first
    #define S second
    using namespace std;
    typedef long long ll;
    typedef pair<int,int>p;
    //ll n,q,a[MAXN],k[MAXN],sum[MAXN];
    int main()
    {
        int n;
        ll a[2010];
        while(scanf("%d",&n))
        {
            memset(a,0,sizeof(a));
            if(n==0)break;
            for(int i=0;i<n;i++)
                cin>>a[i];
            sort(a,a+n);
            ll cnt=0;
            for(int i=0;i<n;i++)
                for(int j=i+1;j<n;j++)
            {
                ll tmp=a[i]+a[j];
                cnt+=n-(upper_bound(a,a+n,tmp)-a);
                //cout<<cnt<<endl;
            }
            cout<<cnt<<endl;
        }
        return 0;
    }
    

      题解:先排序成有序的,再利用upperbound函数找出大于两数(两边)之和的数,统计共有几个,因为已经排序,秩序找到第一个大于两边之和的,即可找到全部大于两边之和的。

  • 相关阅读:
    python中的operator.itemgetter函数
    win10下安装Cygwin配置gcc编译环境
    英文单词拼写纠错
    关于文本处理之结巴分词
    mysql 主从复制
    YII2.0 增删改查
    Linux screen命令简介
    Zookeeper简介与安装
    如何在Linux上安装Tomcat
    Java学习笔记之自定义异常
  • 原文地址:https://www.cnblogs.com/passion-sky/p/8997604.html
Copyright © 2020-2023  润新知