Pairs
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3431 Accepted Submission (s): 1229 problem Description
John had n points on the X axis, and their coordinates is (x[i],0), (i=0,1,2,..., n−1) (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) |x[b]−x[a]|≤k. (A Input
The first line contains a single integer T (about 5), indicating the number of cases.
Each test case is begins with the integers n,k (1≤n≤100000,1≤k≤109).
Next n lines contain an integer x[i] (−109≤x[i]≤109) x[i] (−109≤x[i]≤109), means the x x coordinates. Output
For each case, output a integer means how many pairs <a,b> that |x[b]−x[a]|≤k. |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
Ten Source
Bestcoder Round #31
Test instructions: First, a group of samples, give you a N and K, representing the array of n number (not strictly ascending, not to be confused by the sample), set an array of x array, ask you how many pairs of coordinates <a,b> , so x[b]-x[a] <= K. Range: 1≤n≤105,1≤k≤109 1≤n≤10^5,1≤k≤10^9
Analysis: First of all consider can be sorted, the answer is yes, because it requires only the logarithm of the subscript, rather than the logarithm of the true value, and then the order has been done, then consider the scale method, first the right hand backward movement until X[r]-x[l] >= K, Also need a special sentence is equal, if the equivalent ans + = (r-l), otherwise ans + = (r-l-1), for what, the manual simulation can be, and then l++, in fact, is a change a little board problem.
Then we find this relationship, in fact, we use two points is also possible, we enumerate the left endpoint, and then two points down the right end, of course, we have to change the special sentence
Note: The answer may be large oh, remember to long long
Comparison:
It can be found that the ruler takes a little bit faster. Ruler take reference code
Ruler take
#include <bits/stdc++.h>
#define LL Long long
using namespace std;
const int N = 1e5 + ten;
ll A[n];
int main () {
ios_base::sync_with_stdio (0);
int t;cin>>t;
while (t--) {
int n;ll k;cin>>n>>k;
for (int i = 1;i <= n;i++) cin>>a[i];
Sort (a+1,a+n+1);
if (A[n]-a[1] <= k) {cout<<n* (n-1)/2<<endl;continue;}
ll ans = 0;
int l,r;
ll t = 0;
L = R = 1;
while (true) {
while (A[r]-a[l] < K && R < N) r++;
if (r = = n && a[r]-a[l] <= k) {ans + 1ll* (r-l) * (r-l+1)/2;break;}
else if (A[r]-a[l] = = k) ans + = 1ll* (r-l);
else if (A[r]-a[l] > k) ans + = 1ll* (R-L-1);
l++;
if (L = = n) break;
}
cout<<ans<<endl;
}
return 0;
}
Two-part reference code
#include <bits/stdc++.h>
#define LL Long long
using namespace std;
const int N = 1e5 + ten;
ll A[n];
int main () {
ios_base::sync_with_stdio (0);
int N;ll k;
int t;cin>>t;
while (t--) {
cin>>n>>k;
for (int i = 1;i <= n;i++) cin>>a[i];
Sort (a+1,a+1+n);
ll ans = 0;
for (int i = 1;i < n;i++) {
int t = lower_bound (a+1,a+n+1,a[i]+k)-A;
if (A[t]-a[i] = = k) ans + = 1ll* (t-i);
else ans + = 1ll* (t-i-1);
}
Cout<<ans<<endl;}
return 0;
}
If you have any errors or omissions, please talk to up,thx in private.