You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N]; //存储人员能力值
bool b[N]; //桶,用来记录是否使用过
int MIN = 0x3f3f3f3f;//组中最少人数值
int n;
int main() {
cin >> n;
//输入实力值并排序
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);//贪心算法,都是排序,从小到大
//遍历每个学生
for (int i = 1; i <= n; i++) {
if (!b[i]) { //此学生没有加入到组中
b[i] = true; //准备把他加入到某个组中
int c = a[i];
//对于当前学生的后面每个学生
for (int j = i + 1; j <= n + 1; j++) { //只有n+1这么写最后一个才能加进来执行这个循环
if (!b[j]) { //如果此人没有加入到组中
if (a[j] - c != 1 && a[j] != c) { //与前一个差不是1而且与前端的也不一样这时表示前面的分组就已经结束了需要画句号了
MIN = min(MIN, j - i); //完成本次分组,看看长度是多少
break; //本次结束
} else if (a[j] - c == 1) {
c = a[j]; //持续本次分组中
b[j] = true; //标识
}
}
}
}
}
//输出大吉
cout << MIN << endl;
return 0;
}