二分

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. mysql 中文配置(转)

    Dos下连接mysql后,运行一下几项就可以插入中文了: SET character_set_client = gbk; SET character_set_connection = gbk; SET ...

  2. Find the Celebrity 解答

    Question Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...

  3. java保留有效数字

    DecimalFormat df=DecimalFormat("######0.0") double d = df.format(xx): 保留一位就是"######0. ...

  4. dev grdicontrol 根据条件改变行颜色,字体颜色等

    dev grdicontrol 根据条件改变行背景   dev grdicontrol 根据条件改变字体颜色  dev grdicontrol 根据条件改变某个单元格背景 不用写代码  打开gridc ...

  5. 介绍一款Android小游戏--交互式人机对战五子棋

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6589025 学习Android系统开发之余,编 ...

  6. Hibernate PO对象的状态

    Hibernate的PO对象有三种状态:临时状态(又称临时态).持久状态(又称为持久态)和脱管状态(又称为脱管态.游离态).处理持久态的对象也称为PO,临时对象和脱管对象也称为VO. 1.临时态: 简 ...

  7. oracle 10g 数据库字符集更改

    1.更改数据库字符集为GBK SHUTDOWN IMMEDIATE; STARTUP MOUNT EXCLUSIVE; ALTER SYSTEM ENABLE RESTRICTED SESSION;A ...

  8. JS回调函数的使用和作用

    <html> <head> <title>回调函数(callback)</title> <script language="javasc ...

  9. bower安装使用以及git安装

    bower需要:node 和 git node安装包下载:http://blog.csdn.net/myan/article/details/2028545 Git安装: 选择第二项:Use Git ...

  10. Web 1三级联动 下拉框 2添加修改删除 弹框

    Web  三级联动 下拉框 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...