lightoj 1307 Counting Triangles (二分)

原创 2016年08月30日 10:53:58

Description

You are given N sticks having distinct lengths; you have to form some triangles using the sticks. A triangle is valid if its area is positive. Your task is to find the number of ways you can form a valid triangle using the sticks.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing an integer N (3 ≤ N ≤ 2000). The next line contains N integers denoting the lengths of the sticks. You can assume that the lengths are distinct and each length lies in the range [1, 109].

Output

For each case, print the case number and the total number of ways a valid triangle can be formed.

Sample Input

3

5

3 12 5 4 9

6

1 2 3 4 5 6

4

100 211 212 121

Sample Output

Case 1: 3

Case 2: 7

Case 3: 4


题意:输入n个长度,问能构成的三角形的个数。

用二分查找来写。

#include<cstdio>
#include<algorithm>
using namespace std;
long long a[2005]; 
int main()
{
	int t,flag=1;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
		}
		sort(a,a+n);
		int ans=0; 
		for(int i=0;i<n-2;i++)
		{
			for(int j=i+1;j<n-1;j++)
			{
				long long sum=a[i]+a[j];
				int p=lower_bound(a+j,a+n,sum)-a;
				ans+=p-1-j;   //不算p所在位置 
			}
		}
		printf("Case %d: %d\n",flag++,ans);
	} 
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LightOj 1307 - Counting Triangles(二分)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1307 1307 - Counting Triangles ...

【lightoj1307】Counting Triangles

J - J 圆周率用acos(-1.0) 使用longlong Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format...

Light OJ:1307 Counting Triangles(二分+暴力枚举)

1307 - Counting Triangles     PDF (English) Statistics Forum Time Limit...

UVALive 3295 Counting Triangles(组合计数)

题意:给出一个m×n的网格点中能组成的三角形的个数。 思路:直接算很难入手,可以发现任意三个点可以组成一个三角形当且仅当三点不共线,因此可以用总的方案数-三点共线的方案数。三点形成的直线是水平或者垂...

Uva 12075 - Counting Triangles 解题报告

Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triang...
  • kbdwo
  • kbdwo
  • 2014-04-06 20:46
  • 934

HDU1936--Counting Triangles

Description Given an equilateral triangle with n the length of its side, program to count how man...

UVALive 3295 Counting Triangles

UVALive 3295 Counting Triangles 链接:UVALive 3295 题意:给出一个M*N的矩阵,问取其任意三点最多能取多少不完全重叠的三角形? 思路:这题与UVALive ...

lightoj1058 Parallelogram Counting

思路:一个二维坐标系中给出n个点,可以两两连线,问这些所有线段中能组成多少平行四边形。n 这里显然不能枚举组合,那样是n^4的做法,必然是超时的。那么我们可以用平四边形的等价定义,两条相互平分的线...

【LightOJ - 1058】Parallelogram Counting

Parallelogram Counting Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

LightOJ 1058 & Poj 1971 Parallelogram Counting

题目链接1 http://lightoj.com/volume_showproblem.php?problem=1058 题目链接2 http://poj.org/problem?id=1971 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)