Light OJ:1307 Counting Triangles(二分+暴力枚举)
来源:互联网 发布:fisher调节阀计算软件 编辑:程序博客网 时间:2024/06/11 08:21
![](http://lightoj.com/images/submit1.png)
![](http://lightoj.com/images/udebug.png)
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
Output for Sample Input
3
5
3 12 5 4 9
6
1 2 3 4 5 6
4
100 211 212 121
Case 1: 3
Case 2: 7
Case 3: 4
PROBLEM SETTER: JANE ALAM JAN
题目大意:给了你n个边长,保证每个边长都是唯一的,输出可组成多少种三角形。
解题思路:普通三重for循环肯定超时。这里我们将边长排序,两个for循环控制两个最小边,然后二分计算后面有多少条边小于这两个边即可。
代码如下:
#include <cstdio>#include <algorithm>using namespace std;int n;int tmp;int a[2010];int main(){int t;scanf("%d",&t);int kcase=1;while(t--){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&a[i]);}sort(a,a+n);//将边排序 int cnt=0;for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){tmp=a[i]+a[j];//枚举两个短边之和 int pos=lower_bound(a+j,a+n,tmp)-a;//找他大于的第三边的下标 if(pos==j)//找不到不加 {continue;}cnt=cnt+(pos-j-1);//找到的话,算出个数,加上去 }}printf("Case %d: %d\n",kcase++,cnt);}return 0;}
- Light OJ:1307 Counting Triangles(二分+暴力枚举)
- LIGHT OJ 1307 - Counting Triangles【二分枚举】
- 【light-oj】-’1307 - Counting Triangles(二分)
- Light OJ 1307 - Counting Triangles(lower_bound)
- light oj 1307 - Counting Triangles (二分--三角形个数)
- lightoj 1307 Counting Triangles | 二分/暴力
- lightoj 1307 Counting Triangles | 二分/暴力
- LOJ 1307 - Counting Triangles(二分)
- lightoj 1307 Counting Triangles (二分)
- 【LightOJ】1307 - Counting Triangles(二分)
- LightOj 1307 - Counting Triangles(二分)
- uva 585 - Triangles(暴力枚举)
- UVA585- Triangles(暴力枚举)
- ZOJ3944People Counting<暴力/枚举>
- Codeforce 552D Vanya and Triangles(暴力枚举)
- LOJ 1307 Counting Triangles
- 1307 - Counting Triangles
- Lightoj 1307 Counting Triangles
- excel模板制作注意点
- ICE简单介绍及使用示例
- LightOJ 1331:求面积
- sqlserver2008出现数据库主体在该数据库中拥有架构,无法删除的解决方案
- 非容器组件_Demo01.java
- Light OJ:1307 Counting Triangles(二分+暴力枚举)
- Apache ZooKeeper
- 提高数据库查询效率的有效方法
- [转]hls之m3u8、ts、h264、AAC流格式详解
- Eclipse 配置 tomcat
- java反射要点
- root工具类
- Ubuntu14.04下安装google chrome浏览器
- PHP 字符串二进制相互转换