Question
每个人都有对应的价值,个人里选个人,要求这个人里任意两个人的差值小于。求
Solution
sort deque
排个序,从小到大将元素放入双端队列队尾,若头尾差大于,则去掉队头,记录最大的队伍元素个数即可。
Code
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int>P; const double eps = 1e-8; const int NINF = 0xc0c0c0c0; const int INF = 0x3f3f3f3f; const ll mod = 1e9 + 7; const ll maxn = 1e6 + 5; const int N = 2e5 + 5; ll n,k,a[N]; void solve(){ cin>>n>>k; for(int i=1;i<=n;i++){ cin>>a[i]; } sort(a+1,a+1+n); deque<int>q; ll ans=0; for(int i=1;i<=n;i++){ q.push_back(a[i]); while(q.back()-q.front()>k) q.pop_front(); ans=max(ans,(ll)(q.size())); } cout<<ans<<'\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int T;cin>>T; while(T--){ solve(); } return 0; }