`
zsybupt
  • 浏览: 41575 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

boj 320

    博客分类:
  • oj
阅读更多
 

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);
	}
}

 

分享到:
评论

相关推荐

    BOJ题目1023. Ancient Keyboard 源代码

    BOJ的题目1023. Ancient Keyboard解法 源代码

    boj 0809复试模拟题答案

    boj 上08 09 年复试模拟题的答案

    boj:算法

    boj:算法

    JAVA_BOJ

    JAVA_BOJ

    Algorithm-BOJ.zip

    Algorithm-BOJ.zip,BekJon在线法官(Java,Kotlin,SWIFT)和PS路线图,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    BOJ

    BOJ

    Algorithm-BOJ-PSJ.zip

    Algorithm-BOJ-PSJ.zip,Baykon在线判断JAVA问题解决方法(第二章),算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-BOJ-AutoCommit.zip

    Algorithm-BOJ-AutoCommit.zip,当您解决baekjoon online judge的问题时,它会自动提交并推送到远程存储库。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-boj-auto-submit.zip

    Algorithm-boj-auto-submit.zip,日本央行cli提交脚本,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Python库 | boj-0.0.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:boj-0.0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Boj Coloring Book-crx插件

    通过这本图画书展示您的创造力,其中包括Boj和朋友。 一本有趣的,全数字化且可重复使用的着色书,可用于 通过这本图画书展示您的创造力,其中包括Boj和朋友。 一本有趣的全数字可重复使用的图画书,专为孩子,父母...

    boj.kr:解决boj.kr的问题

    解决问题 Boj.kr

    BOJ:日本央行

    BOJ:日本央行

    boj:算法求解

    boj:算法求解

Global site tag (gtag.js) - Google Analytics