二分

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. Word Pattern II 解答

    Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  2. MySQL 索引优化全攻略

    所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后根据特征值来快速查找.而用的最多,并且是mysql默认的就是二叉树算法 BTREE, ...

  3. java与.net比较学习系列(2) 基础语言要素

    这一篇从最基础的开始对比总结,说起基础语言要素,故名思义,就是学习语言的基础,主要内容包括标识符,关键字和注释.我想从以下几点进行总结,有区别的地方有都使用红色粗体字进行了总结. 1,标识符 2,关键 ...

  4. html_day3

    总结学习html的第一天 表格的结构说明 <table></table> <tr></tr> <td></td> <th& ...

  5. php 二维码生成类

    <?php /** * BarcodeQR - Code QR Barcode Image Generator (PNG) * @package BarcodeQR * @category Ba ...

  6. ajax局部刷新分页

    //请求数据加载绑定页面 function DindAjax(pageIndex) {//获取参数 var colors = $("#colorsVal").val(); $.aj ...

  7. UIScrollView 代理方法

    在使用UIScrollView和它的子类UITableView时,有时需要在不同操作状态下,做不同的响应. 如何截获这些状态,如正在滚动,滚动停止等,使用UIScrollViewDelegate_Pr ...

  8. Spark IDEA开发环境构建

    本文档基于IEDA构建spark maven应用. date: 2016/8/1 author: wangxl 1.下载IDEA https://www.jetbrains.com/idea/ 2.安 ...

  9. (原)Ubuntu16中安装cuda toolkit

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5655957.html 参考网址: https://devtalk.nvidia.com/default ...

  10. 做了一个js的拉动遮罩层,两个图片分别显示的效果

    想做成车修好了和没修好的对比,所以需要两个图片.需要用到的知识点, 1.定位 2.mouse 的事件(代码中体现) 3.鼠标指针的移动距离算法 4.css中,cursor的应用 好了,废话不多说 ,直 ...