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. java文件的I/O

    [原创] java文件的I/O操作,简单来说就是向文件中写入数据以及从文件中读出数据,这是我们平日做的最多的操作,这里给出两种文件I/O操作,当然还有许多的操作方法,各种流的使用,可谓是高深莫测:不管 ...

  2. 【SSH】——Struts2中的动态方法调用(二)

    当action中的方法有很多时,那应该怎么调用呢?上次我们提到的UserAction类中只有一个execute方法,如果我们需要增加用户的增删改查方法,如下: public class UserAct ...

  3. [C/C++] “箭头(->)”和“点号(.)”的区别

    转自:http://blog.csdn.net/gyymen/article/details/4962873 首先介绍一下C++中的结构.对于一个结构: struct MyStruct { int m ...

  4. C#判断字符串是否为数字字符串

    在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现.[方法一]:使用 try{} catch{} 语句.      我们可以在try语句块中试图将str ...

  5. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. 【BZOJ 2753 滑雪与时间胶囊】

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2843  Solved: 993[Submit][Status][Discuss] Descripti ...

  7. [fzu 2273]判断两个三角形的位置关系

    首先判断是否相交,就是枚举3*3对边的相交关系. 如果不相交,判断包含还是相离,就是判断点在三角形内还是三角形外.两边各判断一次. //http://acm.fzu.edu.cn/problem.ph ...

  8. poj 2104 (主席树写法)

    //求第K的的值 1 #include<stdio.h> #include<iostream> #include<algorithm> #include<cs ...

  9. synchronized ---- 作用

    获得同步锁: 1.清空工作内存: 2.从主内存拷贝对象副本到工作内存: 3.执行代码(计算或者输出等): 4.刷新主内存数据: 5.释放同步锁.

  10. JAX-WS 注解

    一.概述 “基于 XML 的 Web Service 的 Java API”(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注 ...