pairs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2037    Accepted Submission(s): 732

Problem Description
John has n points on the X axis, and their coordinates are (x[i],0),(i=0,1,2,…,n−1). He wants to know how many pairs<a,b> that |x[b]−x[a]|≤k.(a<b)
 
Input
The first line contains a single integer T (about 5), indicating the number of cases.
Each test case begins with two integers n,k(1≤n≤100000,1≤k≤109).
Next n lines contain an integer x[i](−109≤x[i]≤109), means the X coordinates.
 
Output
For each case, output an integer means how many pairs<a,b> that |x[b]−x[a]|≤k.
 
Sample Input
2
5 5
-100
0
100
101
102
5 300
-100
0
100
101
102
 
Sample Output
3
10
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5733 5732 5731 5730 5729 
 
 
题意:给出一个含有n个元素的数组 ,要求 |a[j]-a[i]|<=k && i<j 问在这个序列中有多少个二元组满足这个条件??
题解:将两个条件化一下可以得到 1, a[i] - k <= a[j] <= a[i] + k  2, i < j
所以我们可以利用 c++ 提供的工具 lower_bound 和 upper_bound
1.lower_bound 返回第一个大于等于当前元素的第一个数的下标.
2.upper_bound 返回第一个小于等于当前元素的第一个数的下标.
所以用这个可以分别找到大于等于 a[i] - k 和小于等于 a[i]+k 的第一个位置.
然后判断一下下界是否大于 i ,如果不是则 l = i+1
判断一下上界是否大于 i ,如果不是直接continue。最后,计数即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = ;
LL a[N];
int main()
{
int tcase;
int n;
LL k;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%lld",&n,&k);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+,a+n+);
LL cnt = ;
for(int i=;i<=n;i++){
int l = lower_bound(a+,a++n,a[i]-k)-a;
int r = upper_bound(a+,a++n,a[i]+k)-a;
if(l<=i) l = i+;
if(r>=i){
cnt+=(r-l);
}
}
printf("%lld\n",cnt);
}
return ;
}

hdu 5178(二分-lower_bound,upper_bound)的更多相关文章

  1. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  2. lower_bound && upper_bound

     用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...

  3. [STL]lower_bound&upper_bound

    源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...

  4. STL中的unique()和lower_bound ,upper_bound

    unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...

  5. C++ lower_bound/upper_bound用法解析

    1. 作用           lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...

  6. hdu 4024 二分

    转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html   一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...

  7. lower_bound/upper_bound example

    http://www.cplusplus.com/reference/algorithm/upper_bound/左闭右开 Return iterator to lower bound Returns ...

  8. HDU 5178 pairs(双指针)

    HDU 5178 pairs(双指针) Hdu 5178 解法:因为要求的是绝对值小于等于k,因此数字的序号关系并不重要,那么排序后使用双指针即可解决这个问题. #include<queue&g ...

  9. vector 牛逼 +lower_bound+ upper_bound

    vector 超级 日白 解决的问题空间问题,可以自由伸缩. 一下用法: 向量大小: vec.size(); 向量判空: vec.empty(); 末尾添加元素: vec.push_back(); / ...

随机推荐

  1. Intellij Idea 创建Web项目入门

    相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.baidu.com/s/1dEstJ5f Tomcat(ap ...

  2. delphi RGB与TColor的转换

    1.RGB转换为Tcolor function RGBToColor(R,G,B: byte): Tcolor;begin  Result := B Shl 16 or G  shl 8 or R;e ...

  3. iBatis的基本使用

    项目结构: 依赖jar: 数据库依赖: CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, PRIMA ...

  4. BZOJ 3668:起床困难综合症(贪心)

    分析:按位贪心即可. program sleep; var a,g:..]of longint; n,i,m,ans,t,len,x,y,v:longint; c:char; s:string; e: ...

  5. NOIP临考经验【转】

    NOIP临考经验 1.提前15分钟入场,此时静坐调整心态,适当的深呼吸 2.打开编辑器并调整为自己喜欢的界面 3.熟悉文件目录,写好准确无误的代码模板 4.压缩包或许还不能解压,但是文件名已经可以知道 ...

  6. transitionEnd和animationEnd的一个临时解决方案

    transtionEnd需要添加前缀,并且存在多次触发问题,animationEnd也需要添加前缀,下面是一个临时性解决方案,解决了部分问题,完美方案探索中 (function(){ var body ...

  7. [学习笔记]最小割之最小点权覆盖&&最大点权独立集

    最小点权覆盖 给出一个二分图,每个点有一个非负点权 要求选出一些点构成一个覆盖,问点权最小是多少 建模: S到左部点,容量为点权 右部点到T,容量为点权 左部点到右部点的边,容量inf 求最小割即可. ...

  8. readelf用法小记

    By francis_hao    Feb 14,2017 显示ELF文件的信息 用法概述 readelf和objdump类似,不过,readelf会显示更详细的信息,而且独立于BFD库,因此当BFD ...

  9. 修改firefox默认下载路径

    菜单栏---编辑---首选项--在常规页就可以看到下载设置了

  10. 精通JS正则表达式(转)

    精通JS正则表达式,讲的比较详细,学习正则表达式的朋友可以参考下.    正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡 ...