boj 320

 

Description
Recently, Teoy bought some matches of different length. He knew that he could use these matches to form a triangle. But soon he got tired of it. Now he wanted to reckon the number of triangles could be formed using these matches. He thought about it long and hard but found no solution. So turn to you for help.

Input
You will be given a number t of cases. (0<t<=10)
For each case, is an integer n, represents the first line is the number of the matches. (2<n<=3000)
The following line are n integers, represent the length of each match.(0<ai<10^9)

Output
For each case, you should output the number of triangles can be formed.

Sample Input
2
5
1 1 1 1 1
6
4 3 1 2 6 5


Sample Output
10
7

 

Source
3230391

 

 

拿oj当编译器用了。。。

给出一组数,判断能组成三角形的可能数。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
int arr[3005];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		long long num=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d",&arr[i]);
		sort(arr,arr+n);
		for(int i=0;i<n-2;i++)
		{
			long long sum = 0;
			int bound = i+2;
			for(int j=i+1;j<n-1;j++)
			{
				if(bound==j)bound++;
				for(int p=bound;p<n;p++)
				{
					if(arr[i]+arr[j]>arr[p])sum++;
					else
					{
						bound=p;
						break;
					}
				}
				if(arr[i]+arr[j]>arr[n-1])bound=n;
				if(j-1>i&&arr[i]+arr[j-1]>arr[j])sum--;
				num+=sum;
			}
		}
		printf("%lld\n",num);
	}
}

 

你可能感兴趣的:(BO)