hdu 5178(二分-lower_bound,upper_bound)
pairs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2037 Accepted Submission(s): 732
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.
5 5
-100
0
100
101
102
5 300
-100
0
100
101
102
10
#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)的更多相关文章
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- lower_bound && upper_bound
用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...
- [STL]lower_bound&upper_bound
源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...
- STL中的unique()和lower_bound ,upper_bound
unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...
- C++ lower_bound/upper_bound用法解析
1. 作用 lower_bound和upper_bound都是C++的STL库中的函数,作用差不多,lower_bound所返回的是第一个大于或等于目标元素的元素地址,而upper ...
- hdu 4024 二分
转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html 一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...
- lower_bound/upper_bound example
http://www.cplusplus.com/reference/algorithm/upper_bound/左闭右开 Return iterator to lower bound Returns ...
- HDU 5178 pairs(双指针)
HDU 5178 pairs(双指针) Hdu 5178 解法:因为要求的是绝对值小于等于k,因此数字的序号关系并不重要,那么排序后使用双指针即可解决这个问题. #include<queue&g ...
- vector 牛逼 +lower_bound+ upper_bound
vector 超级 日白 解决的问题空间问题,可以自由伸缩. 一下用法: 向量大小: vec.size(); 向量判空: vec.empty(); 末尾添加元素: vec.push_back(); / ...
随机推荐
- Intellij Idea 创建Web项目入门
相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.baidu.com/s/1dEstJ5f Tomcat(ap ...
- 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 ...
- iBatis的基本使用
项目结构: 依赖jar: 数据库依赖: CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, PRIMA ...
- BZOJ 3668:起床困难综合症(贪心)
分析:按位贪心即可. program sleep; var a,g:..]of longint; n,i,m,ans,t,len,x,y,v:longint; c:char; s:string; e: ...
- NOIP临考经验【转】
NOIP临考经验 1.提前15分钟入场,此时静坐调整心态,适当的深呼吸 2.打开编辑器并调整为自己喜欢的界面 3.熟悉文件目录,写好准确无误的代码模板 4.压缩包或许还不能解压,但是文件名已经可以知道 ...
- transitionEnd和animationEnd的一个临时解决方案
transtionEnd需要添加前缀,并且存在多次触发问题,animationEnd也需要添加前缀,下面是一个临时性解决方案,解决了部分问题,完美方案探索中 (function(){ var body ...
- [学习笔记]最小割之最小点权覆盖&&最大点权独立集
最小点权覆盖 给出一个二分图,每个点有一个非负点权 要求选出一些点构成一个覆盖,问点权最小是多少 建模: S到左部点,容量为点权 右部点到T,容量为点权 左部点到右部点的边,容量inf 求最小割即可. ...
- readelf用法小记
By francis_hao Feb 14,2017 显示ELF文件的信息 用法概述 readelf和objdump类似,不过,readelf会显示更详细的信息,而且独立于BFD库,因此当BFD ...
- 修改firefox默认下载路径
菜单栏---编辑---首选项--在常规页就可以看到下载设置了
- 精通JS正则表达式(转)
精通JS正则表达式,讲的比较详细,学习正则表达式的朋友可以参考下. 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡 ...