-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdem_cap.cpp
More file actions
38 lines (38 loc) · 770 Bytes
/
dem_cap.cpp
File metadata and controls
38 lines (38 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
void slove(){
int n,k;
cin>>n>>k;
int a[n];
long long dem=0;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
deque <int> q;
for(int i=0;i<n;i++)
{
if(q.empty()) q.push_back(a[i]);
else if(a[i]-q.front()<k)
{
dem+=q.size();
q.push_back(a[i]);
}
else
{
while(a[i]-q.front()>=k&&!q.empty()) q.pop_front();
dem+=q.size();
q.push_back(a[i]);
}
}
cout<<dem << "\n";
}
int main (){
ios_base::sync_with_stdio(0);cin.tie(0);
int t;
cin >> t;
while(t--){
slove();
}
return 0;
}