传送门:牛客
题目描述:
你的团队中有 n 个人,每个人有一个能力值 ai,现在需要选择若干个人组成一个团队去参加比赛,
由于比赛的规则限制,一个团队里面任意两个人能力的差值必须要小于等于 k ,为了让更多的人有
参加比赛的机会,你最多能选择多少个人参加比赛?
输入:
1
5 3
8 3 5 1 6
输出:
3
一道一星的模拟题,直接暴力枚举可做
主要思路:
直接设置双指针一个i,一个j,不断的去扫每一个区间,然后每段符合标准得区间取一下长度的max就是最后的答案
注意点:注意枚举时的边界问题(一个小细节)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x=0,w=1;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*w;
}
#define maxn 1000000
int a[200002];
int main() {
int T;T=read();
int n,k;
while(T--) {
memset(a,0,sizeof(a));
n=read();k=read();
for(int i=1;i<=n;i++) {
a[i]=read();
}
sort(a+1,a+n+1);
int i=1,j=2;int ans=-99;
while(i<=n) {
while(a[j]-a[i]<=k) {
j++;
if(j>n) break;
}
ans=max(ans,j-i);
if(j>n) break;
i++;
}
cout<<ans<<endl;
}
return 0;
}