1307-counting Triangles
|
PDF (中文版) |
Statistics |
Forum |
| Time Limit:2 second (s) |
Memory limit:32 MB |
You is given N sticks having distinct lengths; You have a to form some triangles using the sticks. A Triangle is valid if it 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 is 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 the lengths is 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 is 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 Test Instructions: How many triangles can be formed on your side. Idea: First violent combination of two side, then two points to query the third edge can be.
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>Ten#include <map> One using namespacestd; AtypedefLong LongLL; - intans[2005]; -typedefstructpp the { - Short intx; - Short inty; - } SS; +SS ak[4000005]; - intMainvoid) + { A inti,j,k; atscanf"%d",&k); - ints; - for(s=1; s<=k; s++) - { - intn,m; -scanf"%d",&n); in for(i=0; i<n; i++) - { toscanf"%d",&ans[i]); + } -Sort (ans,ans+n); the intCnt=0; * for(i=0; i<n; i++) $ {Panax Notoginseng for(j=i+1; j<n; J + +) - { theak[cnt].x=i; +ak[cnt].y=J; Acnt++; the } + } -LL sum=0; $ for(i=0; i<cnt; i++) $ { - intL=0; - intr=n-1; the intmaxx=Max (ans[ak[i].x],ans[ak[i].y]); - intminn=min (ans[ak[i].x],ans[ak[i].y]);Wuyi intId=0; theL=0; -r=n-1;intid1=-1; Wu while(l<=R) - { About intMid= (L+R)/2; $ if(ans[mid]+minn>Maxx) - { -id1=mid; -r=mid-1; A } + ElseL=mid+1; the } -L=0; $r=n-1;intid2=-1; the while(l<=R) the { the intMid= (L+R)/2; the if(ans[mid]<maxx+Minn) - { inId2=mid; theL=mid+1; the } About Elser=mid-1; the } the if(id1!=id2) the { +sum+=id2-id1+1; - if(ak[i].x>=id1&&ak[i].x<=id2) thesum--;Bayi if(ak[i].y<=id2&&ak[i].y>=id1) thesum--; the } - } -printf"Case %d:", s); theprintf"%lld\n", sum/3); the } the return 0; the}
1307-counting Triangles