日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

pairs

發布時間:2023/12/15 综合教程 44 生活家
生活随笔 收集整理的這篇文章主要介紹了 pairs 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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): 4888Accepted Submission(s): 1758

Problem Description
John hasnpoints 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 integerT(about 5), indicating the number of cases.
Each test case begins with two integersn,k(1≤n≤100000,1≤k≤109).
Nextnlines contain an integerx[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:64476446644564446443

二分水題

 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

總結

以上是生活随笔為你收集整理的pairs的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。