刷题记录:牛客NC204859组队

传送门:牛客

题目描述:

你的团队中有 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值