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

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

BestCoder Round #31

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 返回第一个大于等于当前元素的第一个数的下标.

详见:http://blog.csdn.net/niushuai666/article/details/6734403

2.upper_bound 返回第一个小于等于当前元素的第一个数的下标.

详见:http://blog.csdn.net/niushuai666/article/details/6734650

所以用这个可以分别找到大于等于 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 = 100005;
LL a[N];
int main()
{
    int tcase;
    int n;
    LL k;
    scanf("%d",&tcase);
    while(tcase--){
        scanf("%d%lld",&n,&k);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        sort(a+1,a+n+1);
        LL cnt = 0;
        for(int i=1;i<=n;i++){
            int l = lower_bound(a+1,a+1+n,a[i]-k)-a;
            int r = upper_bound(a+1,a+1+n,a[i]+k)-a-1;
            if(l<=i) l = i+1;
            if(r>i){
                cnt+=(r-l+1);
            }
        }
        printf("%lld\n",cnt);
    }
    return 0;
}
时间: 2024-07-31 22:02:02

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

STL之二分查找:hdu 5178 ( BestCoder Round #31 1001 )

STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们的作用域是已经排序好的的数组. ★binary_search试图在已排序的[first, last)中寻找元素value.如果找到它会返回true,否则返回false,它不返回查找位置. ★iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素. ★iterat

二分lower_bound()与upper_bound()的运用

<span style="color:#6633ff;">/* G - 二分 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status Description Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each o

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

关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提). Tips:1.在检索前,应该用sort函数对数组进行从小到大排序.     2.使用以上函数时必须包含头文件:#include < algorith

C++标准库之 Lower_Bound, upper_Bound

关于二分查找,这绝对是最简单却又最难的实现了,其各种版本可以参见http://blog.csdn.net/xuqingict/article/details/17335833 在C++的标准库中,便提供了这样的函数,lower_bound 与 upper_bound,对于这两个函数的理解,有如下几种情形: 1   数组中包含至少一个目标元素,例如在下面数字中搜索数字3. 在该数组中搜索数字3,得到的low与high的结果如图所示: 其实这很简单  表示  [ low , high ) 这个半开半

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

Hdu 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T

stl(GCC4.9.3 in MinGW)中rbtree lower_bound/upper_bound 的实现

STL内部实现的rbtree,实现 lower_bound/upper_bound 过程,是从 begin() 开始向 end() 进行遍历,将元素的 key 与目标 key 进行比较,直至找到的第一个符合要求的 iterator 为止!具体看代码,如下 位于bits/stl_tree.h 1 template<typename _Key, typename _Val, typename _KeyOfValue, 2 typename _Compare, typename _Alloc> 3

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in