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 )
 

 

Input
The first line contains a single integer ), means the X coordinates.
 

 

Output
For each case, output an integer means how many pairsk.
 

 

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 
 

二分水题

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <cmath>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <stack>
 9 #include <vector>
10 #include <set>
11 #include <map>
12 typedef long long ll;
13 #define maxn 100005
14 #define lson l,mid,rt<<1
15 #define rson mid+1,r,rt<<1|1
16 using namespace std;
17 
18 
19 ll n,k;
20 ll a[maxn];
21 
22 int main(){
23 
24     int T;
25     while(cin>>T){
26         while(T--){
27             cin>>n>>k;
28             for(int i=1;i<=n;i++){
29                 cin>>a[i];
30             }
31             sort(a+1,a+n+1);
32             ll ans=0;
33             int p;
34             for(int i=2;i<=n;i++){
35                 p=lower_bound(a+1,a+i,a[i]-k)-a;
36                 ans+=i-p;
37             }
38 
39             cout<<ans<<endl;
40         }
41     }
42 
43 }
View Code

相关文章: