题意:给两个数n和d,然后输入n个数,问最少要删掉几个数才能让剩下的n个数的任意两个数相差不大于d
AC代码:
代码语言:javascript复制#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXN 105
#define MAX(a,b) a>b?a:b
using namespace std;
int n,d,mx;
int pre[MAXN];
int main()
{
mx = -1;
scanf("%d%d",&n,&d);
for(int i=0;i<n;i ) scanf("%d",&pre[i]);
sort(pre,pre n);
for(int i=0;i<n;i ){
for(int j=i;j<n;j ){
if(pre[j] - pre[i] <= d){
mx = MAX(mx,j-i 1);
}
}
}
printf("%dn",n - mx);
return 0;
}