1307 - Counting Triangles
Time Limit: 2 second(s) Memory Limit: 32 MB

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
题意:给你的边能构成多少个三角形。
思路:先暴力组合两条边,然后二分查询第三条边即可。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stdlib.h>
7 #include<math.h>
8 #include<stack>
9 #include<vector>
10 #include<map>
11 using namespace std;
12 typedef long long LL;
13 int ans[2005];
14 typedef struct pp
15 {
16 short int x;
17 short int y;
18 } ss;
19 ss ak[4000005];
20 int main(void)
21 {
22 int i,j,k;
23 scanf("%d",&k);
24 int s;
25 for(s=1; s<=k; s++)
26 {
27 int n,m;
28 scanf("%d",&n);
29 for(i=0; i<n; i++)
30 {
31 scanf("%d",&ans[i]);
32 }
33 sort(ans,ans+n);
34 int cnt=0;
35 for(i=0; i<n; i++)
36 {
37 for(j=i+1; j<n; j++)
38 {
39 ak[cnt].x=i;
40 ak[cnt].y=j;
41 cnt++;
42 }
43 }
44 LL sum=0;
45 for(i=0; i<cnt; i++)
46 {
47 int l=0;
48 int r=n-1;
49 int maxx=max(ans[ak[i].x],ans[ak[i].y]);
50 int minn=min(ans[ak[i].x],ans[ak[i].y]);
51 int id=0;
52 l=0;
53 r=n-1;int id1=-1;
54 while(l<=r)
55 {
56 int mid=(l+r)/2;
57 if(ans[mid]+minn>maxx)
58 {
59 id1=mid;
60 r=mid-1;
61 }
62 else l=mid+1;
63 }
64 l=0;
65 r=n-1;int id2=-1;
66 while(l<=r)
67 {
68 int mid=(l+r)/2;
69 if(ans[mid]<maxx+minn)
70 {
71 id2=mid;
72 l=mid+1;
73 }
74 else r=mid-1;
75 }
76 if(id1!=id2)
77 {
78 sum+=id2-id1+1;
79 if(ak[i].x>=id1&&ak[i].x<=id2)
80 sum--;
81 if(ak[i].y<=id2&&ak[i].y>=id1)
82 sum--;
83 }
84 }
85 printf("Case %d: ",s);
86 printf("%lld\n",sum/3);
87 }
88 return 0;
89 }

1307 - Counting Triangles的更多相关文章

  1. hdu 1396 Counting Triangles(递推)

    Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...

  2. Counting Triangles(hd1396)

    Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. UVA 12075 - Counting Triangles(容斥原理计数)

    题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...

  4. LA 3295 (计数 容斥原理) Counting Triangles

    如果用容斥原理递推的办法,这道题确实和LA 3720 Highway很像. 看到大神们写的博客,什么乱搞啊,随便统计一下,这真的让小白很为难,于是我决定用比较严格的语言来写这篇题解. 整体思路很简单: ...

  5. UVALive 3295 Counting Triangles

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. UVA 12075 Counting Triangles

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)

    先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[ ...

  8. Codestorm:Counting Triangles 查各种三角形的个数

    题目链接:https://www.hackerrank.com/contests/codestorm/challenges/ilia 这周六玩了一天的Codestorm,这个题目是真的很好玩,无奈只做 ...

  9. HDUOJ-Counting Triangles

    Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. .Net Core——用SignalR撸个游戏

    之前开内部培训,说到实时web应用这一块讲到了SignalR,我说找时间用它做个游戏玩玩,后面时间紧张就一直没安排.这两天闲了又想起这个事,考虑后决定用2天时间写个斗D主,安排了前端同学写客户端,我写 ...

  2. 同步阻塞IO模型

    同步阻塞IO模型 有上篇IO模型中的,同步阻塞IO模型,我们能够知道,用户线程发起请求后就一直阻塞的等待 内核完成准备数据.数据拷贝的工作.并且返回成功的指示. 实现 使用java来实现同步阻塞IO模 ...

  3. 零基础学习java------30---------wordCount案例(涉及到第三种多线程callable)

    知识补充:多线程的第三种方式 来源:http://www.threadworld.cn/archives/39.html 创建线程的两种方式,一种是直接继承Thread,另外一种就是实现Runnabl ...

  4. 一起手写吧!Promise!

    1.Promise 的声明 首先呢,promise肯定是一个类,我们就用class来声明. 由于new Promise((resolve, reject)=>{}),所以传入一个参数(函数),秘 ...

  5. 【leetcode】917. Reverse Only Letters(双指针)

    Given a string s, reverse the string according to the following rules: All the characters that are n ...

  6. Linux 双网卡绑定及Bridge

    Linux 双网卡绑定及Bridge 阅读(5,202) 一:linux操作系统下双网卡绑定有七种模式.现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时又能做相应的冗余,可以说是好处多多. ...

  7. 详解 Java I/O 与装饰者模式

    1.I/O分类与装饰者模式 基本java I/O包含两种类型的流,字节流(inputStream.outputStream)与字符流(Writer,Reader),关于I/O操作类的设计,用到了装饰者 ...

  8. python3约瑟夫环问题

    问题描述:n个人围成一个圈,从第一个人开始数1,数到第k个出局,然后下一个人继续从1数,求出局人编号 思路:将所有人编号放到数组里,一个人出局后,下一个人加上k对数组长度求余,得出下一个要删除的编号. ...

  9. <转>libevent使用demo

    这篇文章介绍下libevent在socket异步编程中的应用.在一些对性能要求较高的网络应用程序中,为了防止程序阻塞在socket I/O操作上造成程序性能的下降,需要使用异步编程,即程序准备好读写的 ...

  10. [BUUCTF]PWN——wustctf2020_getshell1/2

    wustctf2020_getshell 附件 步骤: 例行检查,32位程序,开启了NX保护 本地试运行一下程序,看看大概的情况 32位ida载入,习惯性的检索程序里的字符串,发现了后门函数 shel ...