Codeforces Round 924 (Div. 2)

B. Equalize

image
image
image

与数组的原始顺序无关,直接排序,然后用双指针滑动范围a[r]-a[l]小于n

#include<bits/stdc++.h>
#define int long long
using namespace std;
void solve(){
	int n;
	cin>>n;
	set<int>st;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		st.insert(x);
	}
	vector<int>a(st.begin(),st.end());
	int m=a.size();
	int ans=1;
	int l=0,r=0;
	while(r<m){
		while(r<m&&a[r]-a[l]<n)r++;
		ans=max(ans,r-l);
		l++;
	}
	cout<<ans<<"\n";
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	for(int i=1;i<=t;i++)solve();
	return 0;
} 
posted @ 2024-02-13 11:46  yufan1102  阅读(27)  评论(0编辑  收藏  举报