Codeforces Round #466 (Div. 2) A. Points on the line[数轴上有n个点,问最少去掉多少个点才能使剩下的点的最大距离为不超过k。]

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
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 1, 9 and 10. The remaining points will have coordinates 3, 4 and 6, so the diameter will be equal to 6 - 3 = 3.

 

[题意]:数轴上有n个点,最少去掉多少个点才能使剩下的点的最大距离为不超过k

[分析]:排序后选的一定是段连续的区间,枚举左右端点即可.排序后n^2枚举区间。一般求合法个数的在区间上做文章.

先排序。元素较少,直接暴力枚举每一个元素num[i],left=i-1记录该元素左边的数目,然后找第一个大于等于num[i]+d的元素num[j],用right=n-j记录下该元素右边的数目。找到最小的(left+right)就是要删除的数目。

[代码]:

/*
题意:给两个数n和d,然后输入n个数,问最少要删掉几个数才能让剩下的n个数的任意两个数相差不大于d
*/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mem(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=150;
const int inf=0x3f3f3f3f;
int main()
{
    int n,d,a[maxn],ans=1e9;
    cin>>n>>d;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)
        {
            if(a[j]-a[i]<=d) ans=min(ans,i-1+n-j);//满足条件:序列中最小值和最大值相差<=d
        }
    }
    cout<<ans<<endl;
}
双重枚举

 

posted @ 2018-02-25 21:45  Roni_i  阅读(263)  评论(0编辑  收藏  举报