pairs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4157    Accepted Submission(s): 1481

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

题意

有x个数,求出这x个数中有多少对数相减的绝对值小于等于k

思路

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int a[maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int t;
int n,k;
cin>>t;
while(t--)
{
cin>>n>>k;
for (int i = 1; i <=n; ++i)
{
cin>>a[i];
}
sort(a+1,a+n+1);
ll ans=0;
for(int i=1;i<=n;i++)
{
int l=lower_bound(a+1+i,a+1+n,a[i]-k)-a-1;
int r=upper_bound(a+i+1,a+1+n,a[i]+k)-a-1;
ans+=r-l;
}
cout<<ans<<endl;
}
return 0;
}

HDU 5178:pairs(二分,lower_bound和upper_bound)的更多相关文章

  1. HDU 5178 pairs(双指针)

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

  2. HDU 5178 pairs —— 思维 + 二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others)     ...

  3. LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]

    给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...

  4. HDU 5178 pairs【二分】||【尺取】

    <题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...

  5. hdu 5178 pairs(BC第一题,,方法不止一种,,我用lower_bound那种。。。)

    题意: X坐标上有n个数.JOHN想知道有多少对数满足:x[a]-x[b]<=k(题意给)[a<b] 思路: 额,,,直接看代码吧,,,, 代码: int T,n,k; int x[100 ...

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

    <span style="color:#6633ff;">/* G - 二分 Time Limit:2000MS Memory Limit:32768KB 64bit ...

  7. hdu 5178 pairs

    pairs 问题描述 John 在X轴上拥有nn个点,他们的坐标分别为$(x[i],0),(i=0,1,2,…,n-1)$. 他想知道有多少对< a,b ><a,b>满足|x[ ...

  8. hdu 5178 pairs (线性探查问题)

    Problem Description John has n points on the X axis, and their coordinates are (x[i],),(i=,,,…,n−). ...

  9. hdu 5178(二分-lower_bound,upper_bound)

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

  10. 二分搜素——(lower_bound and upper_bound)

    因为每个人二分的风格不同,所以在学习二分的时候总是被他们的风格搞晕.有的人二分风格是左闭右开也就是[L,R),有的人是左开右闭的(L,R]. 二分的最基本条件是,二分的序列需要有单调性. 下面介绍的时 ...

随机推荐

  1. ubuntu 安装 openssh-server,xinetd,vmware tools

    ●安装SSH服务 ●判断是否安装ssh服务,通过如下命令:ps -e|grep ssh 1, sudo apt-get update --->软件的更新地址变了,需要更新本机的sources.l ...

  2. LeetCode--122--卖卖股票的最佳时机II

    问题描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易( ...

  3. Jenkins安装以及邮件配置

    Jenkins介绍 Jenkins是一个java开发的.开源的.非常好用持续集成的工具,它能帮我们实现自动化部署环境.测试.打包等等的工作,还可以在构建任务成功或者失败之后给我们发邮件通知. 什么叫持 ...

  4. Linux 强制安装rpm 包

    Linux 强制安装rpm 包 2014年12月12日 10:21 [root@ilearndb1 Server]# rpm -ivh  unixODBC-devel-2.*  --nodeps -- ...

  5. html5- 摘自网友dudu

    HTML5中新增了<canvas>画布标签,通过它,可以使用JavaScript在网页中绘制图像.<canvas>标签在网页中得到的是一个矩形空白区域,可以通过width和he ...

  6. textAlign

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  7. spring cloud图形化dashboard是如何实现指标的收集展示的

    spring cloud图形化dashboard是如何实现指标的收集展示的 1.dashboard图形化界面入口 http://localhost:10000/hystrix.stream 说明:端口 ...

  8. 如何解决请求URL长度超过配置的maxurlLength值问题

    当我们批量请求的数据太多时,会出现请求的url长度超过配置maxurllength值的问题(比如一次性操作1000条数据) 1.问题描述: 我在进行批量选择单据进行发送时,出现这个问题(批量500条) ...

  9. 'PostBuildEvent' failed with error code '1' 'Unspecified error'( PostBuildEvent”失败,错误代码为“1”。“未指定的错误” )

    这种错误很坑,2年前遇到一次,现在有遇到了(主要记不得上次怎么解决了的) 主要在于js文件文件修改保存的时候得以ansi格式保存,不能以utf-8 http://files.cnblogs.com/f ...

  10. bzoj1092

    题解: 按照时间枚举一下那些点有水 然后用物理方案来计算 代码: #include<bits/stdc++.h> ,M=; int n,m,x,y,z,i,A,B,T,g[N],v[M], ...