C. Balanced Team
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
Copy
6 1 10 17 12 15 2
output
Copy
3
input
Copy
10 1337 1337 1337 1337 1337 1337 1337 1337 1337 1337
output
Copy
10
input
Copy
6 1 1000 10000 10 100 1000000000
output
Copy
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).
Problem - 1133C - Codeforceshttps://codeforces.com/problemset/problem/1133/C
#include<iostream>
using namespace std;
#include<string>
#include<algorithm>
#pragma warning (disable:4996)
#include <climits>
#include <vector>
int arr[10000000];
int max(int a, int b);
int main() {
int n;
cin >> n;
for (int cnt = 0; cnt < n; cnt++) {
cin >> arr[cnt];
}
sort(arr, arr + n);
int ans = 0;
for (int cnt = 0,i=1; cnt < n; cnt++) {
int temp = 0;
while (arr[i] - arr[cnt] <= 5 && i < n)
temp++, i++;
ans = max(ans, i - cnt);
}
cout << ans;
return 0;
}
int max(int a, int b) {
return a > b ? a : b;
}
我踩过的坑:因为这道题我修改了下,按照上面内循环的方式的话,就是锁定最后一个,然后如果用temp和ans比的话,就会导致变成另一种内循环的模式.