lightoj 1307 Counting Triangles | 二分/暴力

来源:互联网 发布:c盘windows有多大 编辑:程序博客网 时间:2024/06/05 18:15

题意:

给你N条边,问你能组成三角形的方法数。

思路:

判断三条边能否组成三角形,根据任意两条边的和大于第三边。

实际上,在确定A<=B<=C的情况下,只要A+B的和大于C即可认为ABC能组成三角形。

这题可以不用二分,用二分速度反而变慢了。排好序后乱搞就可以了。

AC代码:

#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <iostream>#include <algorithm>#include <iomanip>using namespace std;const int MAXN = 2005;int a[MAXN];int main(){        ios::sync_with_stdio(false);        int T, cas = 0;        int n;        cin>>T;        while(T--)        {                cin>>n;                for(int i = 0;i < n; i++)                        cin>>a[i];                //                sort(a, a+n);                long long cot = 0;                for(int i = 0;i < n-2; i++)                {                        int k = i;                        for(int j = i+1;j < n-1; j++)                        {                                while(k < n && a[i] + a[j] > a[k])                                        k++;                                cot += k-j-1;                        }                }                cout<<"Case "<<++cas<<": "<<cot<<endl;        }        return 0;}


0 0
原创粉丝点击