1307 - Counting Triangles

   PDF (English) Statistics Forum
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 }
时间: 2024-10-12 12:16:43

1307 - Counting Triangles的相关文章

LightOJ 1307 Counting Triangles 二分查找

[题解整理]二分题 题目类型: 二分查找: 二分答案. 大致解题思路: 查找注意有序和返回值: 浮点数注意精度: 整数注意返回值,建议另外维护一个变量,用于储存可行解. 题目 分类 传送门 WA点 poj 2785 二分查找 题解 lightoj 1088 二分查找 题解 lightoj 1307 二分查找 题解 longlong poj 2456 整数二分答案 题解 poj 3104 整数二分答案 题解 poj 3258 整数二分答案 题解 poj 3273 整数二分答案 题解 lightoj

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

hdu 1396 Counting Triangles (递推)

Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 966 Problem Description Given an equilateral triangle with n the length of its side, program to

uva 12075 - Counting Triangles(容斥原理)

题目链接:uva 12075 - Counting Triangles 题目大意:一个n?m的矩阵,求说有选任意三点,可以组成多少个三角形. 解题思路:任意选三点C(3(n+1)?(m+1))但是有些组合是不可行得,即为三点共线,除了水平和竖直上的组合,就是斜线上的了,dp[i][j]即为ij情况下的斜线三点共线. #include <cstdio> #include <cstring> typedef long long ll; const int N = 1005; ll dp

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

题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对于两点,求他们的gcd - 1,得到的就是他们之间有多少个点,那么情况数就可以求了,然后还是利用容斥原理去计数,然后累加出答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using nam

SPOJ Problem 1724:Counting Triangles

题目大意:数三角形.. 数据范围是一百万,而且暴力不可行,所以要推公式.公式可以参照 http://www.docin.com/p-720073077.html #include<cstdio> int t; long long n,m,ans; int main(){ scanf("%d",&t); while(t--){ scanf("%lld",&n); ans=(n*(n+1)*(2*n+1))/6; m=(n-1)/2; if

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

先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[i][j]代表在这个矩形中终点是到(i,j)这个点的满足题意的直线条数,那么,用dp的话就可以得出递推关系:由长和宽分别小1的左右两个矩形中满足题意的线的条数减去他们共有的矩形中满足的线的条数(容斥减去重复部分),之后还要判断从最左上角的点(1,1)到(i,j)是否可以组成一条线,这个条件是gcd(

zoj 1629 - Counting Triangles

题目:统计三角形个数. 分析:dp,数学公式. 递推公式: 正面:f[n] = f[n-1] + n*(n+1)/2 = f[n-1] + n*n/2 + n/2 =(n*(n+1)*(2*n+1)+3*n*(n+1))/12 反面:F[ n ] = F[ n-2 ] + n*(n-1)/2 说明:两种方法都试一下(⊙_⊙).(2011-09-21 12:45) #include<iostream> #include<stdlib.h> using namespace std; i

HDU1396:Counting Triangles -DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1396 //根据每增加一条边所增加的正三角和倒三角的个数构造方程 #include<cstdio> #include<cstring> __int64 dp[510]; int main() { int n; memset(dp,0,sizeof(dp)); dp[1]=1; for(int i=2;i<=500;i++) { if(i&2==1) { dp[i]=dp[i