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 |
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的更多相关文章
- hdu 1396 Counting Triangles(递推)
Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...
- Counting Triangles(hd1396)
Counting Triangles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA 12075 - Counting Triangles(容斥原理计数)
题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...
- LA 3295 (计数 容斥原理) Counting Triangles
如果用容斥原理递推的办法,这道题确实和LA 3720 Highway很像. 看到大神们写的博客,什么乱搞啊,随便统计一下,这真的让小白很为难,于是我决定用比较严格的语言来写这篇题解. 整体思路很简单: ...
- UVALive 3295 Counting Triangles
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVA 12075 Counting Triangles
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)
先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[ ...
- Codestorm:Counting Triangles 查各种三角形的个数
题目链接:https://www.hackerrank.com/contests/codestorm/challenges/ilia 这周六玩了一天的Codestorm,这个题目是真的很好玩,无奈只做 ...
- HDUOJ-Counting Triangles
Counting Triangles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- Navicat连接Linux系统下的Mysql数据库
1 . 进入Linux机器 , 登录并进入mysql如果没有安装mysql,参照 https://blog.csdn.net/weixin_35353187/article/details/81712 ...
- 日常Java 2021/10/11
抽象类 所有对象都是通过类描述的,但不是所有的类都是用来描述对象,就好比抽象类,此类中没有足够的信息描述一个对象. 抽象类不能实例化对象,所以抽象类必须的继承,才可以使用. 抽象方法 Abstract ...
- javascript的原型与原型链
首先套用一句经典名言,JavaScript中万物皆对象. 但是对象又分为函数对象和普通对象. function f1(){}; var f2=function(){}; var f3=new Func ...
- 机器学习常用python包
(py37) ai@ai:~$ pip freeze |grep -v '@' astor==0.8.1 certifi==2021.5.30 chardet==4.0.0 cycler==0.10. ...
- ViewStub应用
在开发应用程序的时候,会遇到这样的情况,在运行时动态的根据条件来决定显示哪个View或哪个布局,可以把可能用到的View都写在上面,先把他们的可见性设置为View.GONE,然后在代码中动态的更改它的 ...
- EBS 抓trace 文件
如果要对FORM的操作做TRACE操作,可以使用 帮助->诊断->跟踪 中启用跟踪功能来实现. 但是如果要实现对并发请求的trace,需要在 系统管理员->并发->方案-> ...
- zabbix实现对主机和Tomcat监控
#:在tomcat服务器安装agent root@ubuntu:~# apt install zabbix-agent #:修改配置文件 root@ubuntu:~# vim /etc/zabbix/ ...
- Javaj基础知识runtime error
遇到的java 运行时错误: NullPointerException空指针 ,简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始 ...
- 【编程思想】【设计模式】【行为模式Behavioral】Publish_Subscribe
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/publish_subscribe.py #!/usr/b ...
- 『学了就忘』Linux服务管理 — 76、RPM包安装的服务管理
目录 1.独立服务的启动管理 2.独立服务的自启动管理 方式一: 方式二:(推荐) 方式三: 3.验证 1.独立服务的启动管理 (1)使用/etc/init.d/目录中的启动脚本启动服务(推荐) [r ...