欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Points on the line

程序员文章站 2022-03-29 17:51:58
...
A. Points on the line

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
Note

In the first test case the optimal strategy is to remove the point with coordinate 4. The remaining points will have coordinates 1 and 2, so the diameter will be equal to 2 - 1 = 1.

In the second test case the diameter is equal to 0, so its is unnecessary to remove any points.

In the third test case the optimal strategy is to remove points with coordinates 19 and 10. The remaining points will have coordinates 34 and 6, so the diameter will be equal to 6 - 3 = 3.

百度翻译:

我们没有测试用例。一场大型奥林匹克竞赛即将来临。但problemsetters的首要任务应该是增加了另一个问题的轮。
线路上多点的直径是最大的两点之间的距离从这组。例如,在 multiset { 1,3,2 直径, 1 } 2。
多组由一个点的直径是0。
你在直线上得到n分。你必须删除点的最小数目是多少,所以,其余点的直径不超过multiset D?
输入
第一行包含两个整数n和d(1 ≤ N ≤ 100, 0 ≤ D ≤ 100)-点数量和允许的最大直径分别。
第二行包含N个整数(1 ≤ xi ≤ 100)-点的坐标。

这很明显是枚举呀!

1到100试一遍就好了

#include<bits/stdc++.h>
using namespace std;
int s[105];
int main()
{
	int n,i,d,a,j,ans=0;
	scanf("%d%d",&n,&d);
	for(i=1;i<=n;i++){
		scanf("%d",&a);
		s[a]++;
	}
	for(i=1;i<=100;i++)		//枚举坐标 
	{
		if(s[i]!=0){		//如果有这个点 
			int t=0;		
			for(j=max(1,i-d);j<=i;j++)
				t+=s[j];//求出直径内点的个数 
			ans=max(t,ans);	//求一个在右边已d为直径能包括点的最多点 
		}
	}
	printf("%d",n-ans);		//除了能包括的,就是要删除的 
	return 0;
}