Codeforces 1928B Equalize

problem link

Given the feature of numerically adding a permutation to a sequence, elements of different values can become the same as long as their difference is strictly less than n n n.

This conclusion can be easily proven if we construct the worst case scenario: how to turn n n n consecutive numbers into the same value by adding a permutation. To do that, simply add the permutation in reverse order to achieve unanimous value. Moreover, it is evident that any subproblem of this form (less than n n n elements within the value range of n n n) can be solved in the same way.

Therefore, we can greedily implement a two-pointer algorithm that maintains an interval where the values differ less than n n n.

Additionally, the above method fails completely when the value of each element is not unique (i.e. duplicates). However, notice that duplicates can never result in the same value after adding a permutation. So, we simply get rid of all duplicates before the starting the above process.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int Maxn=2e5+10;
int a[Maxn];
int n;
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
int main()
{
	// freopen("in.txt","r",stdin);
	int T=read();
	while(T--)
	{
		n=read();
		for(int i=1;i<=n;++i)
		a[i]=read();
		sort(a+1,a+1+n);
		int r=1,ans=1;
		int cnt=unique(a+1,a+1+n)-a-1;
		for(int i=1;i<cnt;++i)
		{
			r=max(r,i);
			while(r<cnt && a[r+1]-a[i]<n)++r;
			ans=max(ans,r-i+1);
		}
		printf("%d\n",ans);
	}
	return 0;
}
  • 24
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值