二分求解 三角形 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
#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的应用 涉及范围的二分查找可以先求上界再算下界,结果即上界减下界的更多相关文章
- 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+...+ ...
- C++ STL中的Binary search(二分查找)
这篇博客转自爱国师哥,这里给出连接https://www.cnblogs.com/aiguona/p/7281856.html 一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返 ...
- HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)
#include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- 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 ...
- POJ-2456.Aggressivecows.(二分求解最大化最小值)
本题大意:在坐标轴上有n个点,现在打算在这n个点上建立c个牛棚,由于牛对厂主的分配方式表示很不满意,它很暴躁,所以它会攻击离它很近的牛来获得快感,这件事让厂主大大知道了,他怎么可能容忍?所以他决定有策 ...
- 算法学习->求解三角形最小路径
00 问题 00-1 描述 对给定高度为n的一个整数三角形,找出从顶部到底部的最小路径和.每个整数只能向下移动到与之相邻的整数. 找到一个一样的力扣题:120. 三角形最小路径和 - 力扣(LeetC ...
- 算法学习->求解三角形最小路径及其值
00 问题 00-1 描述 对给定高度为n的一个整数三角形,找出从顶部到底部的最小路径和.每个整数只能向下移动到与之相邻的整数. 找到一个一样的力扣题:120. 三角形最小路径和 - 力扣(LeetC ...
- hdoj 2899 Strange fuction【二分求解方程】
Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- Find the Celebrity 解答
Question Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...
- Java 自定义实现 LRU 缓存算法
背景 LinkedHashMap继承自HashMap,内部提供了一个removeEldestEntry方法,该方法正是实现LRU策略的关键所在,且HashMap内部专门为LinkedHashMap提供 ...
- 【转】手机web——自适应网页设计(html/css控制)
手机web——自适应网页设计(html/css控制) 就目前形势来看,Web App 正是眼下的一个趋势和潮流,但是,对于Web App的设计可能大家有的不是很了解,下面就将整理好的网页设计的技巧奉献 ...
- Linux usb子系统(一):子系统架构
一.USB协议基础知识 前序:USB概念概述 USB1.0版本速度1.5Mbps(低速USB) USB1.1版本速度12Mbps(全速USB) USB2.0版本速度480Mbps(高速USB). ...
- SSH方式登录github出现Permission denied (publickey)
今天在公司上传了代码,回到家pull,结果竟然出现了“Permission denied (publickey)“这种东西.第一反应是key不对,可是上次明明用key登录过,不可能不对啊,难道是文件被 ...
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...
- Direct3D 索引缓存
小学的时候我们知道3个顶点组成一个三角形,那么四个顶点我们会说有4个三角形.这就是一个顶点同时参与了四次绘制三角形的结果. 在程序中也一样,比如我们绘制的两个三角形是挨着一起的,总有几个顶点是重合的. ...
- Spark常用函数讲解之键值RDD转换
摘要: RDD:弹性分布式数据集,是一种特殊集合 ‚ 支持多种来源 ‚ 有容错机制 ‚ 可以被缓存 ‚ 支持并行操作,一个RDD代表一个分区里的数据集RDD有两种操作算子: Trans ...
- (3)选择元素——(5)为项目列表加样式(Styling list-item levels)
Let's suppose that we want the top-level items, and only the top-level items, to be arranged horizon ...
- http://blog.csdn.net/luoshengyang/article/details/6651971
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6651971 在Android系统中,提供了独特 ...