C. BALANCED TEAM[双头指针]

http://www.yyycode.cn/index.php/2020/05/19/c-balanced-team%e5%8f%8c%e5%a4%b4%e6%8c%87%e9%92%88/

https://codeforces.com/problemset/problem/1133/C

You are a coach at your local university. There are nn students under your supervision, the programming skill of the ii-th student is aiai.

You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than 55.

Your task is to report the maximum possible number of students in a balanced team.Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of students.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is a programming skill of the ii-th student.Output

Print one integer — the maximum possible number of students in a balanced team.ExamplesinputCopy

6
1 10 17 12 15 2

outputCopy

3

inputCopy

10
1337 1337 1337 1337 1337 1337 1337 1337 1337 1337

outputCopy

10

inputCopy

6
1 1000 10000 10 100 1000000000

outputCopy

1

Note

In the first example you can create a team with skills [12,17,15][12,17,15].

In the second example you can take all students in a team because their programming skills are equal.

In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).


题意:给定一段序列,求能满足任意两个学生的值相差不超过5的最长的子段

思路:尺取法模拟,O(n)过

根据前题的经验,注意只有一个人和n个搜完只有一个的时候特判安全一点.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
typedef long long LL;
LL a[maxn];
int main(void)
{
	LL n;cin>>n;
	for(LL i=1;i<=n;i++)
		cin>>a[i];
	sort(a+1,a+1+n);
	LL res=-1;
	if(n==1) cout<<1<<endl;
	else 
	{
		for(LL i=2,j=1;i<=n&&j<=n; )
		{
			if(a[i]-a[j]<=5)
			{
				res=max(res,i-j+1);
				i++;	
			}	
			if(a[i]-a[j]>5)
			{
				j++;
			}
		}
		if(res==-1) cout<<1<<endl;每一个相差大于5
		else cout<<res<<endl;	
	}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值