pairs

http://acm.hdu.edu.cn/showproblem.php?pid=5178

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

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:  6447 6446 6445 6444 6443 
 

二分水题

 #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
typedef long long ll;
#define maxn 100005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std; ll n,k;
ll a[maxn]; int main(){ int T;
while(cin>>T){
while(T--){
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>a[i];
}
sort(a+,a+n+);
ll ans=;
int p;
for(int i=;i<=n;i++){
p=lower_bound(a+,a+i,a[i]-k)-a;
ans+=i-p;
} cout<<ans<<endl;
}
} }

pairs的更多相关文章

  1. [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  2. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  3. Leetcode-24 Swap Nodes in Pairs

    #24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  4. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  5. 数论 - Pairs(数字对)

    In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...

  6. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  7. Palindrome Pairs -- LeetCode 336

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  8. 336-Palindrome Pairs

    336-Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the ...

  9. Palindrome Pairs

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  10. Calculating Stereo Pairs

    Calculating Stereo Pairs Written by Paul BourkeJuly 1999 Introduction The following discusses comput ...

随机推荐

  1. 【转载】如何在 Github 上发现优秀的开源项目?

    之前发过一系列有关 GitHub 的文章,有同学问了,GitHub 我大概了解了,Git 也差不多会使用了,但是还是搞不清 GitHub 如何帮助我的工作,怎么提升我的工作效率? 问到点子上了,Git ...

  2. ZooKeeper系列(1) 整体介绍(转)

    原文地址:https://www.cnblogs.com/wuxl360/p/5817471.html 一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术. ...

  3. hint之qb_name

    http://www.thinkindata.com/?p=34 该hint用于子查询(query_block)   很多的情况下,如果子查询共用相同的别名(alias), 可以通过设定不同的qb_n ...

  4. HTML(具体代码看笔记本)

    参考:https://www.cnblogs.com/liwenzhou/p/7988087.html 一, HTML   1. HTML结构   2. 标签    1. 块儿级标签     h1~h ...

  5. python之Web服务器案例

    HTTP协议简介 1. 使用谷歌/火狐浏览器分析 在Web应用中,服务器把网页传给浏览器,实际上就是把网页的HTML代码发送给浏览器,让浏览器显示出来.而浏览器和服务器之间的传输协议是HTTP,所以: ...

  6. 15. "wm_concat"_数据库中将查询出来的多条记录中的某个字段用","拼接起来

    例子: select wm_concat(roleid) from lbmember where userid = ? 值的形式:1,2,3 下面是把1,2,3转换为1;2;3select repla ...

  7. JAVA 整合 SSM (Spring + SpringMVC + MyBatis)

    < 一 > POM 配置文件 ( 如果出现 JAR 包 引入错误, 请自行下载 ) <project xmlns="http://maven.apache.org/POM/ ...

  8. shiro用authc配置后登录成功后不能跳转到index页面

    转自:https://ydoing.iteye.com/blog/2248188

  9. EOF与子过程返回

        在2000及其以上系统,P处理语句GOTO新增了:EOF系统标签,意思是移动到当前P处理文件的结尾,EOF==END OF FILE的缩写,意为文件结尾,主要表现形式为:GOTO :EOFOR ...

  10. C# 事件 event 【转】

    C#事件(event)解析   事件(event),这个词儿对于初学者来说,往往总是显得有些神秘,不易弄懂.而这些东西却往往又是编程中常用且非常重要的东西.大家都知道windows消息处理机制的重要, ...