cf 466 div.2 A. Points on the line

A. Points on the line
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round.

The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1, 3, 2, 1} is 2.

Diameter of multiset consisting of one point is 0.

You are given n points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed d?

Input

The first line contains two integers n and d (1 ≤ n ≤ 100, 0 ≤ d ≤ 100) — the amount of points and the maximum allowed diameter respectively.

The second line contains n space separated integers (1 ≤ xi ≤ 100) — the coordinates of the points.

Output

Output a single integer — the minimum number of points you have to remove.

Examples
input
Copy
3 1
2 1 4
output
1
input
Copy
3 0
7 7 7
output
0
input
Copy
6 3
1 3 4 6 9 10
output
3
分析:
一开始想错了,觉得直接找最小的最大的贪心就好。结果wa了,
用暴力直接开个二重循环.依次遍历,找到除了头尾的数字之外的数字个数,取最小的就行,头是每一个数字,尾是第一个比a[i]大d的数字
代码如下:
#include<cstdio>
#include<algorithm>
#define N 1000
int i,j,n,a[N],ans,d;
using namespace std;
 int main()
 {
     for(scanf("%d %d",&n,&d);++i<=n;)
        scanf("%d",&a[i]);
     sort(a+1,a+1+n);
     for(int i=1;i<=n;i++)
     {
         for(int j=i;a[j]<=a[i]+d&&j<=n;j++)
         if(j-i+1>ans)
            ans=j-i+1;
     }
     printf("%d
",n-ans);





     return 0;
 }
原文地址:https://www.cnblogs.com/renxin123/p/8467879.html