二分

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

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h> using namespace std;
typedef long long LL;
LL a[+];
int n; int Binary_search(int x)
{
int l = ,r = n-;
while(l<=r)
{
int mid = (l+r)/;
if(a[mid] == x)
return mid;
else if(a[mid] > x)
r= mid-;
else
l = mid +;
}
return l; } int main()
{
int T;
cin>>T;
int flag = ;
while(T--)
{ cin>>n;
for(int i = ; i < n; i++)
scanf("%lld",&a[i]);
sort(a,a+n);
int ans = ;
for(int i = ; i < n-; i++)
for(int j = i+; j < n; j++)
{
LL sum = a[i] + a[j];
LL cha = abs(a[j] - a[i]); int up = Binary_search(sum)-;
if(up <= j)
continue;
int low = Binary_search(cha);
if(low < j)
low = j;
ans += (up-low); } cout<<"Case "<<++flag<<": "<<ans<<endl; }
return ;
}
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h> using namespace std;
typedef long long LL;
LL a[+];
int n; int Binary_search(int x)
{
int l = ,r = n;
while(l<r)
{
int mid = (l+r)/;
if(a[mid] == x)
return mid;
else if(a[mid] > x)
r= mid;
else
l = mid +;
}
return l; } int main()
{
int T;
cin>>T;
int flag = ;
while(T--)
{ cin>>n;
for(int i = ; i < n; i++)
scanf("%lld",&a[i]);
sort(a,a+n);
int ans = ;
for(int i = ; i < n-; i++)
for(int j = i+; j < n; j++)
{
LL sum = a[i] + a[j];
LL cha = abs(a[j] - a[i]); int up = Binary_search(sum)-;
if(up <= j)
continue;
int low = Binary_search(cha);
if(low < j)
low = j;
ans += (up-low); } cout<<"Case "<<++flag<<": "<<ans<<endl; }
return ;
}
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h> using namespace std;
typedef long long LL;
LL a[+];
int n; int main()
{
int T;
cin>>T;
int flag = ;
while(T--)
{ cin>>n;
for(int i = ; i < n; i++)
scanf("%lld",&a[i]);
sort(a,a+n);
int ans = ;
for(int i = ; i < n-; i++)
for(int j = i+; j < n; j++)
{
LL sum = a[i] + a[j];
LL cha = abs(a[j] - a[i]); int up = upper_bound(a+j+,a+n,sum)-a; int low = lower_bound(a+j+,a+n,cha)-a; ans += (up-low); } cout<<"Case "<<++flag<<": "<<ans<<endl; }
return ;
}

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

  1. POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)

    题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ...

  2. C++ STL中的Binary search(二分查找)

    这篇博客转自爱国师哥,这里给出连接https://www.cnblogs.com/aiguona/p/7281856.html 一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返 ...

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

    #include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; ...

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

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  5. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)

    A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  6. POJ-2456.Aggressivecows.(二分求解最大化最小值)

    本题大意:在坐标轴上有n个点,现在打算在这n个点上建立c个牛棚,由于牛对厂主的分配方式表示很不满意,它很暴躁,所以它会攻击离它很近的牛来获得快感,这件事让厂主大大知道了,他怎么可能容忍?所以他决定有策 ...

  7. 算法学习->求解三角形最小路径

    00 问题 00-1 描述 对给定高度为n的一个整数三角形,找出从顶部到底部的最小路径和.每个整数只能向下移动到与之相邻的整数. 找到一个一样的力扣题:120. 三角形最小路径和 - 力扣(LeetC ...

  8. 算法学习->求解三角形最小路径及其值

    00 问题 00-1 描述 对给定高度为n的一个整数三角形,找出从顶部到底部的最小路径和.每个整数只能向下移动到与之相邻的整数. 找到一个一样的力扣题:120. 三角形最小路径和 - 力扣(LeetC ...

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

    Strange fuction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. oracle 12g sqlplus安装

    一.下载oracle 12g sqlplus软件 linux 64位操作系统,oracle安装包地址 http://www.oracle.com/technetwork/topics/linuxx86 ...

  2. c++之 常用类型

    C/C++常用类型的范围 C/C++里常用的类型及表示范围如下表所示: 类型 sizeof 表示范围 说明 char 1 -128 - 127 -2^7 - (2^7 - 1) short 2 -32 ...

  3. Android学习总结——去除标题栏

    1.继承app.Activity的Activity去除标题栏 @Override protected void onCreate(Bundle savedInstanceState) { super. ...

  4. C# CheckedListBox控件的使用方法

    1. 加入项 checkedListBox1.Items.Add("蓝色"); checkedListBox1.Items.Add("红色"); checked ...

  5. Unity 图片的灰度处理

    我们平时在做项目时,经常遇到按钮的点击而且还要区分悬浮,点击,禁用的状态,美术要针对一张图片做多个状态图片,资源图片的数量也就增大了,那么打出的包的大小也就跟着上去了,所以我们可以针对原始图片进行Sh ...

  6. Oracle静默安装-简单记录

    一.与图形界面安装一样,检查如下条件:新建用户.组创建安装目录配置环境变量检查安装依赖包修改内核参数……前面这些操作都属于常规操作,不管是图形还是静默都需要处理. 二./home/oracle/dat ...

  7. 一个很经典的this面试题

    !function(){ this.length = 10; var fn = function(){ console.log(this.length); //输出多少? }, arr = [fn, ...

  8. mssql 判断sql语句的执行效率语句

    SET STATISTICS io ONSET STATISTICS time ONgo--========此处为sql代码段=============== select zxbh from t_yr ...

  9. ios 获取屏幕的属性

    屏幕尺寸     CGRect screen = [UIscreen mainScreen].bounds 状态栏尺寸  CGRect rect = [[UIApplication sharedApp ...

  10. (原)caffe中通过图像生成lmdb格式的数据

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5909121.html 参考网址: http://www.cnblogs.com/wangxiaocvp ...