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. Spring IOC - 控制反转(依赖注入) - 配置初始化和销毁的方法

    在Spring中如果某个bean在初始化之后,或销毁之前要做一些额外操作可以为该bean配置初始化和销毁的我方法,在这些方法中完成需要的功能. 实验: 通过断点调试模式,测试初始化方法和销毁方法的执行 ...

  2. Find substring with K-1 distinct characters

    参考 Find substring with K distinct characters Find substring with K distinct characters(http://www.cn ...

  3. [转]NSIS:使用SectionSetFlags根据不同环境自动勾选特定区段

    转自: http://www.flighty.cn/html/bushu/20140526_232.html   在微软SQL2000+SP4集成安装版安装包中可以根据目标操作系统自动勾选对应的版本, ...

  4. ubuntu18.04修改时区

    运行如下命令: sudo tzselect 然后选择亚洲Asia,继续选择中国China,最后选择北京Beijing. 然后创建时区软链 sudo ln -sf /usr/share/zoneinfo ...

  5. 【Python编程:从入门到实践】chapter4 操作列表

    chapter4 操作列表 4.1 遍历整个列表 magicians=['alice','david','carolina'] for magician in magicians: print(mag ...

  6. 给iOS开发新手送点福利,简述UIImagePickerController的属性和用法

    1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;         // 检查指定源是否在设备上 ...

  7. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #17 如何使用ext4

    HACK #17 如何使用ext4 本节介绍ext4的编写和挂载方法.开发版ext4的使用方法.ext4是ext3的后续文件系统,从Linux 2.6.19开始使用.现在主要的发布版中多数都是采用ex ...

  8. solr中facet及facet.pivot理解

    Facet['fæsɪt]很难翻译,只能靠例子来理解了.Solr作者Yonik Seeley也给出更为直接的名字:导航(Guided Navigation).参数化查询(Paramatic Searc ...

  9. python之集合操作

    1.集合的增操作 add(item):增加至集合中 update(set):  若不存在,更新至集合中 2.集合的删操作(五种) pop():  随机删除并返回值 remove(item): 删除va ...

  10. python写批量weblogic爆破脚本

    前言: 整理笔记的时候,发现了weblogic的攻击方法.心里打着算盘看看怎么写 个批量的弱口令爆破脚本.得出了以下思路 思路: 1.利用钟馗之眼采集weblogic的网站,将IP写入到txt 2.添 ...