二分求解 三角形 stl的应用 涉及范围的二分查找可以先求上界再算下界,结果即上界减下界

二分

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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

3

5

3 12 5 4 9

6

1 2 3 4 5 6

4

100 211 212 121

Sample Output

Case 1: 3

Case 2: 7

Case 3: 4

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <math.h>
 5
 6 using namespace std;
 7 typedef long long LL;
 8 LL a[2000+5];
 9 int n;
10
11 int Binary_search(int x)
12 {
13     int l = 0,r = n-1;
14     while(l<=r)
15     {
16         int mid = (l+r)/2;
17         if(a[mid] == x)
18             return mid;
19         else if(a[mid] > x)
20             r= mid-1;
21         else
22             l = mid +1;
23     }
24     return l;
25
26 }
27
28 int main()
29 {
30     int T;
31     cin>>T;
32     int flag = 0;
33     while(T--)
34     {
35
36         cin>>n;
37         for(int i = 0; i < n; i++)
38             scanf("%lld",&a[i]);
39         sort(a,a+n);
40         int ans = 0;
41         for(int i = 0; i < n-1; i++)
42             for(int j = i+1; j < n; j++)
43             {
44                 LL sum = a[i] + a[j];
45                 LL cha = abs(a[j] - a[i]);
46
47                 int up = Binary_search(sum)-1;
48                 if(up <= j)
49                     continue;
50                 int low = Binary_search(cha);
51                 if(low < j)
52                     low = j;
53                 ans +=  (up-low);
54
55
56             }
57
58         cout<<"Case "<<++flag<<": "<<ans<<endl;
59
60     }
61     return 0;
62 }

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <math.h>
 5
 6 using namespace std;
 7 typedef long long LL;
 8 LL a[2000+5];
 9 int n;
10
11 int Binary_search(int x)
12 {
13     int l = 1,r = n;
14     while(l<r)
15     {
16         int mid = (l+r)/2;
17         if(a[mid] == x)
18             return mid;
19         else if(a[mid] > x)
20             r= mid;
21         else
22             l = mid +1;
23     }
24     return l;
25
26 }
27
28 int main()
29 {
30     int T;
31     cin>>T;
32     int flag = 0;
33     while(T--)
34     {
35
36         cin>>n;
37         for(int i = 0; i < n; i++)
38             scanf("%lld",&a[i]);
39         sort(a,a+n);
40         int ans = 0;
41         for(int i = 0; i < n-1; i++)
42             for(int j = i+1; j < n; j++)
43             {
44                 LL sum = a[i] + a[j];
45                 LL cha = abs(a[j] - a[i]);
46
47                 int up = Binary_search(sum)-1;
48                 if(up <= j)
49                     continue;
50                 int low = Binary_search(cha);
51                 if(low < j)
52                     low = j;
53                 ans +=  (up-low);
54
55
56             }
57
58         cout<<"Case "<<++flag<<": "<<ans<<endl;
59
60     }
61     return 0;
62 }

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <math.h>
 5
 6 using namespace std;
 7 typedef long long LL;
 8 LL a[2000+5];
 9 int n;
10
11 int main()
12 {
13     int T;
14     cin>>T;
15     int flag = 0;
16     while(T--)
17     {
18
19         cin>>n;
20         for(int i = 0; i < n; i++)
21             scanf("%lld",&a[i]);
22         sort(a,a+n);
23         int ans = 0;
24         for(int i = 0; i < n-1; i++)
25             for(int j = i+1; j < n; j++)
26             {
27                 LL sum = a[i] + a[j];
28                 LL cha = abs(a[j] - a[i]);
29
30                 int up = upper_bound(a+j+1,a+n,sum)-a;
31
32
33                 int low = lower_bound(a+j+1,a+n,cha)-a;
34
35                 ans +=  (up-low);
36
37
38             }
39
40         cout<<"Case "<<++flag<<": "<<ans<<endl;
41
42     }
43     return 0;
44 }

时间: 2024-12-20 00:32:40

二分求解 三角形 stl的应用 涉及范围的二分查找可以先求上界再算下界,结果即上界减下界的相关文章

hdu3586 树形dp+二分求解

http://acm.hdu.edu.cn/showproblem.php?pid=3586 Problem Description In the battlefield , an effective way to defeat enemies is to break their communication system. The information department told you that there are n enemy soldiers and their network w

HDU ACM 1025 Constructing Roads In JGShining&amp;#39;s Kingdom-&amp;gt;二分求解LIS+O(NlogN)

#include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; //dp[i]保存的是长度为i的最长不降子序列的最小尾元素 int BS(int n,int x) //二分查找下标,当x比全部元素小时下标为1,比全部元素大时下标为n+1. { int low,high,mid; low=1,high=n; while(low<=high) { mid=(low+h

二分求解 切绳子 (浮点数易出现精度问题)

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28644   Accepted: 6076 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

UVA - 10487 - Closest Sums (二分求解)

传送:UVA - 10487 10487 Closest Sums Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number. Input Input contains multiple c

STL源码剖析之【二分查找】

ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置. ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置. lower_bound和uppe

UVA - 10341 - Solve It (二分求解)

思路:给你一个公式,求零点,从题目条件可以看出,此函数式是递减的,所以只要从两头往中间二分答案即可,注意精度问题,因为要精确到小数点后4位,<1e-6居然还WA,<1e-9才过,所以说尽量使精度高点 这里e的n次方可以用exp(n)表示,也可以用pow(M_E, n)表示 以下是math.h中定义的一些常量: /* Definitions of useful mathematical constants * M_E - e * M_LOG2E - log2(e) * M_LOG10E - lo

POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

hdoj 2899 Strange fuction【二分求解方程】

Strange fuction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4599    Accepted Submission(s): 3304 Problem Description Now, here is a fuction:  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=

湘潭比赛有感---铩羽之行

去之前,我们很自信,去之后,发现世界很大. 出发前,我们一直很自信,心理一直觉得:题目难点对我们还有优势,出现动态规划我和小黄能能较快的推出状态转移方程,出现裸水题,小孙立马能水了,没什么好担心. 湘潭大,最先感觉的是学校很大很气派(和nyist比),但学校门口出行的人流很小,往里面走有个类似小公园的地方,很漂亮但是也没什么人,心想:周末同学们估计都在寝室睡懒觉,没起来.往信息大楼走,发现这学校的楼很旧,甚至有点破,安静的很,尽然让人生出一种鄙夷的情绪,还不如我们自己的系楼. 热身赛 我们陆续进