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;
}