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?
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 a single integer — the minimum number of points you have to remove.
3 1 2 1 4
1
3 0 7 7 7
0
6 3 1 3 4 6 9 10
3
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.
百度翻译:
我们没有测试用例。一场大型奥林匹克竞赛即将来临。但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;
}
上一篇: PHP中10个不常见却非常有用的函数
推荐阅读
-
IE上网时提示stack overflow at line:0的三种解决办法
-
pip 错误unused-command-line-argument-hard-error-in-future解决办法
-
微信借助端91助手完美导入LINE表情方法
-
添加或删除程序出现提示Value creation failed at line 521如何处理
-
PHP XML error parsing SOAP payload on line 1
-
Notice: Undefined index: page in E:PHP est.php on line 14
-
line-block的运用最小宽度的设置
-
MySQL在command line Client下的一些命令
-
Notice: Undefined index: page in E:PHP est.php on line 14
-
ERROR: Unrecognized command line argument: 'use'